XLSX file get corrupt when write using NPOI latest version

Apr 20, 2015 at 8:19 AM
Hi Team,

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();
    }

May 3, 2015 at 1:30 AM
Cannot offer any help unfortunately. Can only confirm the issue. Depending on your task, you could maybe try using EEPlus library for xlsx files.