nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.75k stars 1.43k forks source link

Error 0x800A03EC when opening with Excel.Application after saving with NPOI #1308

Closed JeanPhilippeLux closed 7 months ago

JeanPhilippeLux commented 7 months ago

NPOI Version 2.7.0

File Type

Upload the Excel File

Reproduce Steps

I open a file with formulas in it. I change the value of 2 cells and after I call XSSFFormulaEvaluator.EvaluateAllFormulaCells(newWorkbook);

after that I open the file with the MS Excel Interop (here is my code)

"Excel.Application excelApp = new();

Excel.Workbook wb = excelApp.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];"

And on the last line the application crash with error code : 0x800A03EC

Issue Description

If I open the file in Excel, Excel says the file is corrupt and he fix it the the message : "remove /xl/calcChain.xml"

I rollback to 2.6.2 and the Excel files are not corrupt anymore.

thank you.

Bykiev commented 7 months ago

Hi, can you please attach original file and corrupted?

JeanPhilippeLux commented 7 months ago

Oh, sorry.

Here you have the 2 files. Hope that will help you.

Corrupt file.xlsx Good file.xlsx

Thank you.

Bykiev commented 7 months ago

It seems to be smth wrong with shared formulas... Can you please also provide your code to modificate the original file?

JeanPhilippeLux commented 7 months ago

Hi, here the code. I clean up it because there is a connection to DB

string filename = "" //the filename goes here.

XSSFWorkbook newWorkbook = new(); XSSFSheet excelSheet = new();

initializeWorkbook(ref filename, ref newWorkbook, ref excelSheet); int rowIndex = 1;

IRow row = excelSheet.CreateRow(rowIndex);

row.CreateCell(0).SetCellValue(12002219); row.CreateCell(1).SetCellValue((double)2.2);

XSSFFormulaEvaluator.EvaluateAllFormulaCells(newWorkbook);

using FileStream fs = new(filename, FileMode.Create, FileAccess.Write); newWorkbook.Write(fs); fs.Close();

PrintMyExcelFile(filename);

//this code initialize the document private void initializeWorkbook(ref string filename, ref XSSFWorkbook newWorkbook, ref XSSFSheet excelSheet) { newWorkbook = new();

 using (FileStream filestream = new(filename, FileMode.Open, FileAccess.Read))
 {
     newWorkbook = new XSSFWorkbook(filestream);
     filestream.Close();
 }

 int rowIndex = 0;

 excelSheet = (XSSFSheet)newWorkbook.GetSheet("A PRODUIRE");

 if (excelSheet != null)
 {
     while (excelSheet.GetRow(rowIndex) != null)
     {
         excelSheet.GetRow(rowIndex).GetCell(0)?.SetCellValue(0);
         excelSheet.GetRow(rowIndex).GetCell(1)?.SetCellValue(0);
         rowIndex++;
     }
 }
 else
 {
     excelSheet = (XSSFSheet)newWorkbook.CreateSheet("A PRODUIRE");
 }

 IRow row = excelSheet.CreateRow(0);

 row.CreateCell(0).SetCellValue("ItemId");
 row.CreateCell(1).SetCellValue("Qté à produire");
 row.CreateCell(2).SetCellValue(SelectedDate.Date);

}

//this code is for printing but crash at the line : Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1]; private void PrintMyExcelFile(string fileName) { Excel.Application excelApp = new();

 Excel.Workbook wb = excelApp.Workbooks.Open(fileName,
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
     Type.Missing, Type.Missing, Type.Missing, Type.Missing);

 Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
 for (int i = 1; i <= wb.Worksheets.Count; i++)
 {
     if (((Excel.Worksheet)wb.Worksheets[i]).Name == "MAIN")
     {
         ws = (Excel.Worksheet)wb.Worksheets[i];
         ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
         break;
     }
 }

 // Cleanup:
 GC.Collect();
 GC.WaitForPendingFinalizers();

 Marshal.FinalReleaseComObject(ws);

 wb.Close(false, Type.Missing, Type.Missing);
 Marshal.FinalReleaseComObject(wb);

 excelApp.Quit();
 Marshal.FinalReleaseComObject(excelApp);

}

Hope you will have the possibility to reproduce the error and fix it.

Bykiev commented 7 months ago

The bug was already fixed in https://github.com/nissl-lab/npoi/pull/1291