C# Interop not closing Excel process after execution
I have the following blocks of code:
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(this.pathToFile);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
I push the xlRange into an array using:
someArray = (System.Object[,])xlRange.Value2;
Afterwards I try to cleanup with:
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
However, even with all four of the excel objects being released with Marshal
, the process stays open and prevents the file from being opened again.
Any ideas as to how to properly clean up Interop
excel objects?
我有类似的问题,并设法在此过程之后关闭应用程序;
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(xlRange);
Marshal.FinalReleaseComObject(xlWorksheet);
xlWorkbook.Close(false, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(xlWorkbook);
xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);
链接地址: http://www.djcxy.com/p/6070.html
上一篇: C#方法无法正常工作