ExcelDataReader is only able to read the 1st column (header/data) from an excel generated by NPOI.
For example the code below, it will only read 1st column "111" with data "444"
Umm, any idea would be nice, thanks in advance.
One odd part is the file size will change after excel.exe (2019) saves it. 4KB -> 10KB, not sure why. *Then, the file is fully readable by ExcelDataReader.
HSSFWorkbook(.xls) also works, hopfully i am only missing very few pieces?
Write:
XSSFWorkbook EWorkbook = new XSSFWorkbook();
XSSFSheet ESheet1 = (XSSFSheet)EWorkbook.CreateSheet("Test");
XSSFRow ERow = (XSSFRow)ESheet1.CreateRow(0);
XSSFCell ECell = (XSSFCell)ERow.CreateCell(0); ECell.SetCellValue("111");
ECell = (XSSFCell)ERow.CreateCell(1); ECell.SetCellValue("222");
ECell = (XSSFCell)ERow.CreateCell(2); ECell.SetCellValue("333");
ERow = (XSSFRow)ESheet1.CreateRow(1);
ECell = (XSSFCell)ERow.CreateCell(0); ECell.SetCellValue("444");
ECell = (XSSFCell)ERow.CreateCell(1); ECell.SetCellValue("555");
ECell = (XSSFCell)ERow.CreateCell(2); ECell.SetCellValue("666");
using (var file2 = new FileStream(@"C:\inetpub\temp.xlsx", FileMode.Create, FileAccess.ReadWrite))
{
EWorkbook.Write(file2);
file2.Close();
}
Read:
excelSpreadSheet = Excel.ExcelReaderFactory.CreateOpenXmlReader(fs);
The following shows how to create an Excel (.xlsx) file with Nuget package NPOI (v2.6.2) and then read the file with Nuget packages ExcelDataReader (v3.6.0) and ExcelDataReader.DataSet (v3.6.0). It's been tested using a Windows Forms App (.NET Framework) v4.8.
Pre-requisites: Download and install the Nuget packages mentioned above.
Create Excel (.xlsx) file using NPOI.
HelperNPOI:
Usage:
Read Excel (.xlsx) file using ExcelDataReader
Note: The code below is adapted from Using ExcelDataReader to read Excel data starting from a particular cell and ExcelDataReader (Github).
HelperExcelDataReader:
Usage:
Output:
Resources: