Thursday, 1 March 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...