Killing Excel.EXE on server

Possible Duplicate:
How to properly clean up Excel interop objects in C#

Suppose a ASP.NET web application generates automated Excel Reports on the server. How do we kill a server-side Excel.EXE once the processing is over. I am raising this purposely, because I believe that the Garbage Collecter does not clean the Excel executable even after the Excel file is closed.

Any pointers would be helpful?


Sorry to say this, and I'm not trying to be smart, but... don't put office on the server!!!

That's if I've understood correctly! :)

EDIT: Even though I've been marked down for this, I will never ever advocate running Office on the server - it has proven way too much of a pain in the ass for me in the past.

Having said that, the same now goes for me and Crystal Reports ;-)


I agree with not running Office on a server. Not that I have any choice in the matter :)

One thing to keep in mind with the taskkill option, is that unless you specifically plan for it (aka - singleton), you may have multiple copies of Excel (or any other Office app) running, and unintentionally close the wrong instance.

Also note that per http://support.microsoft.com/kb/257757

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

As an alternative, there is a product called Aspose Cells that offers a product that is designed to allow you to programmatically work with an Excel sheet in a server environment. As a disclaimer, I have never personally used this product, but I have heard about it from several people I worked with in the past.


I've had more time to think about this answer, and would now recommend using an XML approach with the Open XML Office spreadsheet format.

Heres some good links to get started on building a office document with code. http://msdn.microsoft.com/en-us/magazine/cc163478.aspx http://msdn.microsoft.com/en-us/library/bb735940(office.12).aspx

Just use SSIS on SQL Server. It provides the ability to export to Excel. Don't run office on the server. Alteranatively waste money on aspose or spreadsheetgear.

GC does work your just not using it properly follow this pattern...

   private void killExcel()
        {
          xlApp.Quit();
          Marshal.ReleaseCOMObject(xlApp);
          if(xlApp != null)
          {
            xlApp = null;
          }
          GC.WaitForPendingFinalizers();
          GC.Collect();
          GC.WaitForPendingFinalizers();
          GC.Collect();
        }

get your Excel operational class to implement IDisposable, and then stick killExcel() in the Dispose method.

UPDATE: Also note that sometimes dev will still see Excel.exe running in task manager. Before assuming the above code isn't working, check that the process that is running the code is also closed. In the case of a VSTO or COM addin, check that Word/powerpoint/other excel instance is also closed as there is still a GC root back to the launching process. Once that is closed the Excel.exe process will close.

链接地址: http://www.djcxy.com/p/35642.html

上一篇: 为什么此代码导致Excel无法正常关闭?

下一篇: 在服务器上杀死Excel.EXE