I have downloaded NPOI 2.1.3 latest version from https://npoi.codeplex.com/releases.
When i am writing XLSX file using this dll then program successfully writes the rows (no error comes). but when i try to open the excel file then i get following error:
"Excel cannot open the file 'xyz.xlsx' because the file format or file extension
is not valid. Verify that the file has not been corrupted and the file extension matches the format of the fie."
I have wrote below code to wrote excel. Can you please help on this.
XSSFWorkbook xssfwb = new XSSFWorkbook();
IWorkbook iworkbook;
using (FileStream file = new FileStream(@"D:\XYZ.xlsx", FileMode.Open, FileAccess.Read))
{
hssfwb = new HSSFWorkbook(file);
iworkbook = hssfwb;
file.Close(); }
ISheet sheet1 = xssfwb.GetSheetAt(0);
for (int i = 1; i <= dt.Rows.Count; i++) //dt is a datatable which contain data to write into excel
{
IRow row = sheet1.CreateRow(i + 1);
for (int j = 0; j < 20; j++)
{
row.CreateCell(j).SetCellValue(x++); }
}
using (FileStream file = new FileStream(@"D:\XYZ.xlsx", FileMode.Open, FileAccess.Write))
{
hssfwb.Write(file);
file.Close();
}
Comments: ** Comment from web user: vkarter **
I have similiar problem.
When I create *.xlsx file using following code:
IWorkbook workbook = new XSSFWorkbook();
ISheet CFSheet = workbook.CreateSheet("CF");
FileStream file= File.Create(path);
workbook.Write(file);
file.Close();
And next try to read the file and save content to Dataset. (using: https://exceldatareader.codeplex.com/)
FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
^
Here I get only 1 column from xlsx file where I have 4 columns in this file.
The File(.xlsx) created by NPOI for example have 35 kB but , when I open this file in excel 2013 and save again in the excel. The excel add some kB to file and it grown to 46kB.
Then this file could be read correctly by https://exceldatareader.codeplex.com/ with all headers and columns.