C#Interop执行后不关闭Excel进程

我有以下代码块:

    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;

我使用以下命令将xlRange推入一个数组中:

someArray = (System.Object[,])xlRange.Value2;

之后,我尝试使用以下方式进行清理:

    Marshal.ReleaseComObject(xlRange);
    Marshal.ReleaseComObject(xlWorksheet);

    xlWorkbook.Close();
    Marshal.ReleaseComObject(xlWorkbook);
    xlApp.Quit();
    Marshal.ReleaseComObject(xlApp);

但是,即使所有四个excel对象都与Marshal发布,该进程仍保持打开状态并防止再次打开该文件。

有关如何正确清理Interop excel对象的任何想法?


我有类似的问题,并设法在此过程之后关闭应用程序;

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/6069.html

上一篇: C# Interop not closing Excel process after execution

下一篇: Custom Event is getting called multiple times