0
votes

My program generates an Excel in a loop and sends it by Email. In each loop iteration it should overwrite the Excel, but the Excel is still used by the old Excel process which is why I get a System.IO.IOException exception.

Here is the process:

enter image description here

Here is the Code:

if(File.Exists(PfadXlsx)) File.Delete(PfadXlsx);

        mExcel.Visible = true;
        mExcelWs.SaveAs(PfadXlsx, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, missing, missing, false, false, false, missing, missing, missing);     // Datei speichern

        mExcelWb.Close(true, missing, missing);
        mExcel.Application.Quit();
        mExcel.Quit();

mExcel == Excel.Application mExcelWB == Excel.Workbook mExcel==Excel.Worksheet missing == type.missing

1
Can you generate a new Excel file on each loop? - rs232

1 Answers

0
votes

You should consider NOT using interop for this; there are quite a few c# libraries out there that will directly generate an xlsx without needing excel installed. An xlsx is just an xml file, zipped and renamed, so it works well without needing additional software. Personally, in the past I’ve used EPPlus, but there are a lot to choose from on nuget. By dropping excel interop you solve your problem, and reduce the install requirements of your program. The user can have excel installed all the same, but it won’t be used. They can also use free excel viewers without needing to shell out for office