Does NPOI have support to .xlsx format?

后端 未结 4 621
遇见更好的自我
遇见更好的自我 2020-12-16 23:16

Will NPOI DLL recognize .xlsx file?

Currently I\'m using NPOI 1.2.5 version DLL for Microsoft Excel 97-2003, but I need to access Excel sheets of exten

4条回答
  •  既然无缘
    2020-12-16 23:52

    May be the library didn't had this feature when the original answer(s) was provided, but now you can handle both xls and xlsx using the same code base without checking for file extensions.

    The trick is to use WorkbookFactory class to transparently load both types of files. This will work as long as you are not using special features specific to either version.

    using (FileStream fileStream = File.OpenRead(fullPathToExcelFile)) //fullPathToExcelFile can hold either a xls or xlsx, we don't care
    {
       IWorkbook workbook = WorkbookFactory.Create(fileStream);
       ISheet worksheet = workbook.GetSheet("SampleSheet");
    
       //Now read from the worksheet anyway you like
       var value = worksheet.GetRow(1).GetCell(1);
    }
    

提交回复
热议问题