Thursday, March 1, 2018

c# - Automatically exit generated Excel file correctly

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


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...