Sunday, 30 April 2017

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...