Excel.exe应用程序完成使用时不closures

我目前正在尝试将graphics/图表从.xls作为图像发送。 我可以得到图表/图表发送电子邮件罚款。 我的问题是,当我看到任务pipe理器中有一个“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(); 

好的,编辑我的代码,仍然没有closuresExcel。 但是,当我退出程序的时候它会死亡。

谢谢

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

看来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.collect之后。

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

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

耶终于!

得到它的工作。

我补充说

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

出了我使用EXCEL的function。

 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进程

你可以尝试直接杀死进程。 (过程名称可以用大写字母)

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