Using NPOI like this:
static void xlsx()
{
XSSFWorkbook xsswb;
using (FileStream file = new FileStream(@"test.xlsx", FileMode.Open, FileAccess.Read))
{
xsswb = new XSSFWorkbook(file);
}
ISheet sheet = xsswb.GetSheet("SheetOne");
for (int row = 0; row <= 200; row++)
{
if (sheet.GetRow(row) != null) //null is when the row only contains empty cells
{
for (var cell = 0; cell < 11; cell++)
{
var cellvalue = sheet.GetRow(row).GetCell(cell);
if (cellvalue != null)
{
Console.WriteLine(string.Format("Row {0} {1} = {2}", row, cell, cellvalue.ToString()));
}
else
{
Console.WriteLine("null");
}
}
}
}
}
on a particular non empty workbook, I have no result written on the console: each rows returned is null.
If I create a new xlsx, fill it, and open it with this code, all works well. Other xlsx open well. The issue must be on the particular format of the xlsx I must import. (I cannot share it here since it contains reserved info)
If I open the test.xslx file with Excel, Save As, xlsx file, the above code works well. (the size of the file grows from 37kb to 69kb)
If I rename test.xlsx in test.xls and use HSSFWorkbook
, NPOI correctly complains that the file is indeed a 2007+ excel file and that I should use XSSFWorkBook
instead.
I cannot change the source excel file, since it is produced by a tool and my users should use it without any modification.
Any ideas? Thank you.