I've been using the Microsoft Excel COM object in C# to allow me to open spreadsheets, process them and then upload to a web service, all worked nicely - or so i thought! After a while i noticed the PC running slowly - so i checked Task Manager and was horrified to see hundreds of EXCEL.exe processes running!

As the Excel Application object is a COM object there's no nice Dispose() method to call, and setting to null didn't help either... after some searching i found the answer:
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//Do some stuff here...
excel.Quit();
while (Marshal.ReleaseComObject(excel) != 0) { }
excel = null;
GC.Collect();
GC.WaitForPendingFinalizers();

Some of the posts i found didn't have the last two lines which call the .NET garbage collector - and it didn't solve the problem until i added them. Also some people found they had to call Marshal.ReleaseComObject() on any Workbook or Worksheet objects they'd used (in my case i didn't need to).

Bookmark with :
Digg It! DZone StumbleUpon Technorati Reddit Del.icio.us Newsvine Furl Blinklist
posted @ Monday, October 29, 2007 11:53 AM | in C#

Comments

Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
on 11/23/2007 3:12 PM
Great!!!

I noticed a similar problem when:

The com using program is running and you close excel inbetween. Then an excel-zombie-process is established.

I am using VB.net and with your snippet I now can kill the COM-channels after interacting with excel.

If you close excel after that cleanup, no zombies around, even your program is still running.

Thx for the hint!
Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
Posted by Dean
on 3/5/2008 4:45 PM
Thanks, this is helpful
Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
Posted by PK
on 4/1/2008 7:50 AM
Very Good
Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
Posted by Deepak
on 8/26/2008 6:21 AM
Thanks a lot, it's helpful for me also. I was looking for this.
Once again thanks a lot
Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
Posted by Andy
on 8/29/2008 6:04 PM
Dont forget using System.Runtime.InteropServices;

for the marshalling
Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
Posted by Dustin
on 9/17/2008 6:30 PM
erg, doesn't bloody work for me - very frustrating problem :/
Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
Posted by Dave
on 11/4/2008 9:56 PM
Didn't work for me either...

what are you doing in between the while brackets. As per your code above you do nothing.

while (Marshal.ReleaseComObject(excel) != 0) { }
Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
Posted by D
on 12/10/2008 1:44 AM
If you havent already, try releasing all com objects individually and even set to null as a precaution:

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkbook);
xlsWorkbook = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsRange);
xlsRange = null;

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
xlsApp = null;

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

This was the only way I could get Excel to fully close without ending the app
Gravatar
# re: Killing EXCEL.exe after using Microsoft Excel COM object in C#
Posted by eran
on 12/31/2008 4:19 PM
maybe you can help with that :
i have this object in my c# program
Application excelFile ;
Workbook theWorkbook;
Sheets sheets ;
Worksheet worksheet;
i create them in the object constructor and try to kill them in the destructor
i follow your instructions one by one but the proccess refuse to dieuntil the i close the form i want it dead when i leaving the destructor
thanks
Eran
i use your instractions one by one but

Post Comment

Title *
Name *
Email
Url
Comment *  


Please add 3 and 5 and type the answer here: