When will NULL be returned by GetRow() method?

Aug 16, 2010 at 6:53 AM

                Sheet sheet = workbook.GetSheetAt(i);

                for (int rowIdx = sheet.FirstRowNum; rowIdx < sheet.LastRowNum; rowIdx++)
                    Row row = sheet.GetRow(rowIdx);
                    if (row == null)

                    for (int colIdx = row.FirstCellNum; colIdx < row.LastCellNum; colIdx++)
                        Cell cell = row.GetCell(colIdx);
                        if (cell == null)
                        string content = cell.ToString();


I got row as null by certain rowIdx. The rowIdx is smaller than sheet.LastRowNum. Why null is returned?


Aug 30, 2010 at 11:01 PM
Edited Aug 30, 2010 at 11:01 PM

Use GetRowEnumerator() and GetCellEnumerator(), instead.  Basically, the problem is that row indices could have gaps between them.  You could have five rows with the following RowIndex values: 0, 1, 5, 6, 8.  LastRowNum would be 8.

NPOI is a very thin wrapper around the XLS format, and as such you have to deal with problems like this.  I believe the root cause is that deleting a row from an Excel spreadsheet does not reorder the remaining rows.  If row 5 in the list above was deleted, you'd still have rows 0, 1, 6, and 8.

Sep 21, 2012 at 10:03 AM



I know this thread is quite old but this problem is causing me major issues.  


I need to import a spreadsheet where each column relates to a series of data.  As the spreadsheet has been used for a number of years, plenty of rows and columns have been inserted and removed from the spreadsheet.  This means that using NPOI I have no way to reliably import the data per column and be sure that each cell relates to the same column.


For example columns A and B contain defition terms, C onwards contains the data.  I would expect for each row I'm processing that I want the data from cell[2], but that isn't always true.  


Is there any way to reliably get the data for a particular column using NPOI?