Search code examples
c#npoi

NPOI does not recognized end of file


I am using NPOI library in order to read .xls and .xlsx files.

However I have this issue, the method GetRow() does not return null even when the row is empty.

Here is the code

int idx_row = 1;
IRow currentRow = sheet.GetRow(idx_row);
while (currentRow != null)
{
    JObject jsonData = new JObject();
    jsonData["a"] = sheet.GetRow(idx_row).GetCell(0).StringCellValue.Replace(" ", "");
    // other similar code
    jsonPlateData.Add(jsonData);
    idx_row++;
    currentRow = sheet.GetRow(idx_row);
}

Solution

  • Check the value of sheet.LastRowNum, it's possible that the row seems empty but excel considers it as filled. If so, open the excel file and delete the rows that are "empty".