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 extension .xlsx
also.
Will NPOI support the above?
Code snippet:
static void Main(string[] args) {
XSSFWorkbook xssfwb;
using(FileStream file=new FileStream(
@"C:\Users\347702\Desktop\Hello.xlsx",
FileMode.Open, FileAccess.Read)) {
xssfwb=new XSSFWorkbook(file);
}
ISheet sheet=xssfwb.GetSheet("sheet1");
sheet.GetRow(1048576);
Console.WriteLine(sheet.GetRow(1048576).GetCell(0).StringCellValue);
}
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.
You can read Excel files in .xls and .xlsx extensions with NPOI, you only need to add the next in the using section
The main thing is at the time you open the file, you have to distinguish between the extensions so you use the appropiate componente, and use an ISheet interface so you can reference the sheet independently of the file extension
Once you have the excel object you only need to read it (in NPOI rows and columns are zero based)
To read the cell valur you can use the .ToString() method or the StringCellValue property, but be careful the StringCellValue only works with string cells, with number and date cells it throws an exception.
Yes it does. NPOI 2.0 beta works. Here's a sample code to get you started:
NPOI 2.0 supports xlsx. You can download it from https://npoi.codeplex.com/releases/view/112932