Excel.exe应用程序在使用完成后未关闭

本文关键字:exe 应用程序 Excel | 更新日期: 2023-09-27 17:49:22

我目前正试图通过电子邮件从。xls作为图像的图形/图表。我可以得到图表,然后发邮件。我的问题是,当我在任务管理器中查看时,有一个"EXCEL.EXE"在我调用xlApp.quit()

后仍在运行。

如有任何帮助,不胜感激。

这是我目前使用的代码。

Excel.Application xlApp;
Excel.Workbooks xlBooks;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.ChartObject xlChartObject;
Excel.Chart xlChart;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlBooks = xlApp.Workbooks;
xlWorkBook = xlBooks.Add(Properties.Settings.Default.FileToSend);
xlWorkSheet = xlWorkBook.Sheets[1];
xlWorkSheet.EnablePivotTable = true; 
string filename = System.IO.Path.GetTempFileName();
xlChartObject = xlWorkSheet.ChartObjects(1);
xlChart = xlChartObject.Chart;
xlChart.Export(filename + ".gif");
xlWorkBook.Close(false, misValue, misValue);
xlBooks.Close();
xlApp.Application.Quit();
if (xlChart != null)
    Marshal.ReleaseComObject(xlChart); xlChart = null;
if (xlChartObject != null)
    Marshal.ReleaseComObject(xlChartObject); xlChartObject = null;
if (xlWorkSheet != null)
    Marshal.ReleaseComObject(xlWorkSheet); xlWorkSheet = null;
if (xlWorkBook != null)
    Marshal.ReleaseComObject(xlWorkBook); xlWorkBook = null;
if (xlBooks != null)
    Marshal.ReleaseComObject(xlBooks); xlBooks = null;
if (xlApp != null)
    Marshal.ReleaseComObject(xlApp); xlApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Ok编辑了我的代码,但仍然没有关闭excel。但是,当我退出程序时,它会死亡。

谢谢

Excel.exe应用程序在使用完成后未关闭

您需要释放您使用的每个对象。

似乎xlApp.Workbooks是由未释放的。

作为旁注,也可能是有一个异常,因此您的清理代码遗漏了。

尝试使用try/catch/finally,如下所示:

Excel.Application xlApp = null;
Excel.Workbook xlWorkBook = null;
Excel.Workbooks xlWorkBooks = null;
Excel.Worksheet xlWorkSheet = null;
object misValue = System.Reflection.Missing.Value;
try
{
    xlApp = new Excel.Application();
    xlWorkBooks = xlApp.Workbooks;
    xlWorkBook = xlWorkBooks.Add(Properties.Settings.Default.FileToSend);
    xlWorkSheet = xlWorkBook.Sheets[1];
    xlWorkSheet.EnablePivotTable = true; 
    string filename = System.IO.Path.GetTempFileName();
    xlWorkSheet.ChartObjects("Chart 1").Chart.Export(filename + ".gif");
    xlWorkBook.Close(false, misValue, misValue);
    xlApp.Quit();
}
catch(Exception ex)
{
    // handle error...
}
finally
{
    if (xlWorkSheet != null)
        Marshal.ReleaseComObject(xlWorkSheet);
    if (xlWorkBook != null)
        Marshal.ReleaseComObject(xlWorkBook);
    if (xlWorkBooks != null)
        Marshal.ReleaseComObject(xlWorkBooks);
    if (xlApp != null)
        Marshal.ReleaseComObject(xlApp);
}

尝试添加第二个gc。收集后。

                GC.Collect();
                GC.WaitForPendingFinalizers();   //gc calls finalize on objects
                GC.Collect();                    //collect objects just finalized

mehow提到实现IDisposable。我也推荐这个

终于来了!

get it working.

我添加了

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

的功能,我正在使用EXCEL。

private void runExcelWork()
{
   //xlApp, xlBooks, xlWorksheet etc.. Is defined in this function
   //Do your work with Excel here.
   //Clean all excel objects here.
}
public void runExcel()
{
   runExcelWork();
   //call GC
   GC.Collect();
   GC.WaitForPendingFinalizers();
   GC.Collect();
   //at this point EXCEL.EXE closes
}

感谢所有帮助我解决这个问题的人!

您是否尝试过以下内容:

 xlWorkBook.Close();
 xlApp.Application.Quit(false);
 xlApp = null;

这将清除任何剩余的excel.exe进程

你可以尝试直接终止进程。(Processname可以是大写字母)

try
{
    foreach (var process in Process.GetProcessesByName("excel"))
    {
     process.Kill();
    }
}