Question

I'm working on this program that will read the data in excel file and put it into our database. The program is written in Visual Studio 2010 using C#, and I'm using the NPOI library.

In the past, I was able to read the spreadsheet row by row and cell by cell to get the data, but the new format of the excel file will not allow me to do this easily. (The excel is given by another user, so I can't really make big changes to it).

There are several "tables" in one sheet (using borders and headers for each column name), and I will need to get data mainly from the tables but sometimes outside the tables too.

I was wondering if I were to read the spreadsheet row by row (which is what I'm a bit for familiar with), is there a way I can tell that I have reached a table? Is there a way I can read the "format" of the cell?

What I mean is, for example, "this cell has borders around it so starting this row is a table." or "the text in this cell is bold, so this row is the header row for this new table."

In the past I was only able to read the "text" for the spreadsheet and not the format/style. I've been searching on the internet and I can only find how to set the style for output excel but not how to read the format from input.

Any help is appreciated, thanks!

Was it helpful?

Solution

It would be better to have the various tables in your source workbook defined as named ranges with known names. Then you can get the associated area like this -

using System.IO;
using System.Windows;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

// ...
        using (var file = new FileStream(workbookLocation, FileMode.Open, FileAccess.Read))
        {
          var workbook = new XSSFWorkbook(file);
          var nameInfo = workbook.GetName("TheTable");
          var tableRange = nameInfo.RefersToFormula;
          // Do stuff with the table
        }

If you have no control over the source spreadsheet and cannot define the tables as named ranges, you can read the cell formats as you suggest. Here is an example of reading the TopBorder style -

        using (var file = new FileStream(workbookLocation, FileMode.Open, FileAccess.Read))
        {
            var workbook = new XSSFWorkbook(file);
            var sheet = workbook.GetSheetAt(0);

            for (int rowNo = 0; rowNo <= sheet.LastRowNum; rowNo++)
            {
                var row = sheet.GetRow(rowNo);
                if (row == null) // null is when the row only contains empty cells 
                    continue;
                for (int cellNo = 0; cellNo <= row.LastCellNum; cellNo++)
                {
                    var cell = row.GetCell(cellNo);
                    if (cell == null) // null is when the cell is empty
                        continue;
                    var topBorderStyle = cell.CellStyle.BorderTop;
                    if (topBorderStyle != BorderStyle.None)
                    {
                        MessageBox.Show(string.Format("Cell row: {0} column: {1} has TopBorder: {2}", cell.Row.RowNum, cell.ColumnIndex, topBorderStyle));
                    }
                }
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top