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

时间:2014-03-11 08:42:31

标签: c# excel com

我目前正在尝试将.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();

确定编辑了我的代码但仍然没有关闭excel。 但是当我退出程序时它确实会死掉。

由于

4 个答案:

答案 0 :(得分:0)

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

似乎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);
}

答案 1 :(得分:0)

尝试添加第二个gc.collect。

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

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

答案 2 :(得分:0)

耶终于来了!

搞定了。

我添加了

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
}

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

我希望其他人觉得这很有用。

答案 3 :(得分:-1)

您是否尝试过以下方面的内容:

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

这应该清理剩余的excel.exe进程

您可以尝试直接杀死该进程。 (Processname可以是大写字母)

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