Monday, November 3, 2008

Kill Excel.exe Process in C# .NET

I am trying to get my C# .NET codes to generate reports into excel files. In the Export function, it creates the excel object, adds in the data and then saves the file.

All of this works fine. But for some reason, the excel.exe process is still running when I check from task manager.

I had tried to quit the excel application. I had tried to use InteropServices.Marshal.ReleaseComObject to get rid of them in .NET. But unfortunately, the excel.exe is still sitting in memory.

Finally, I found a way to kill the process in C# .NET. Take a look on the codes below:


using System.Diagnostics;
using System.Collections;




Hashtable myHashtable;
private void btnExport_Click(object sender, EventArgs e)
{
  // get process ids before running the excel codes
  CheckExcellProcesses();

  // export to excel
  ExportDataToExcel();

  // kill the right process after export completed
  KillExcel();
}

private void ExportDataToExcel()
{
  // your export process is here...
}

private void CheckExcellProcesses()
{
  Process[] AllProcesses = Process.GetProcessesByName("excel");
  myHashtable = new Hashtable();
  int iCount = 0;

  foreach ( Process ExcelProcess in AllProcesses) {
    myHashtable.Add(ExcelProcess.Id, iCount);
    iCount = iCount + 1;
  }
}

private void KillExcel()
{
  Process[] AllProcesses = Process.GetProcessesByName("excel");

   // check to kill the right process
  foreach ( Process ExcelProcess in AllProcesses) {
    if (myHashtable.ContainsKey(ExcelProcess.Id) == false)
       ExcelProcess.Kill();
  }

  AllProcesses = null;
}



Hope this helps.

37 comments:

Anonymous said...

thank you!

Anonymous said...

thank you very much, it works excelently....:)

Anonymous said...

Thank yoo so much! Tried everything but only this works!!!

Koshur on February 18, 2009 at 12:20 PM said...

This is just a fantastic way. It solved my application from crashing. Thanks a lot and a lot.

Anonymous said...

Thanks a million!! Works excellently!!

Anonymous said...

Feels like cheating... but i love it anyway.

Vani said...

Its wonderful...worked out perfectly...

Anonymous said...

Hi! Nice post! Very helpful. I'm new in C# programming and I just want to ask if it doesn't accidentally closes other processes? I tried the code and works very well Thank you very much.

SimYing, Ong on July 16, 2009 at 1:27 AM said...

Thanks. It works perfectly in my web application. =)

Anonymous said...

Thank you very much. Since my processing is time-consuming I edited your code slightly, so immediately after looping the first time and adding all the processes IDs to the collection, I then instantiate the application and immediately loop through again setting the new process ID to a variable. I can then take all the time I need before killing my process and not worry that any other Excel processes will be instantiated before then.

Anonymous said...

Thank you very much. With this code i can kill any process from task manager not just excel. Thanks a lot.

Anonymous said...

Thanks a Million!!!
I have been torturing myself for hours!
I've tried so many solutions but none of them worked!

Your code is simple and effective!

Unknown on February 23, 2010 at 6:19 PM said...

Thanks a lot. I was searching for the solution to get rid of excel instances in task manager. This surely helps.

Anonymous said...

Hi, Thanks for the sample.

How to Kill Specific process/excel file?

Thanks

xiaoyu on February 26, 2010 at 9:08 AM said...

Hi, you can based on the ExcelProcess.Id to kill a specific process.

Anonymous said...

yeah. you rock man. thanks you for sharing. just search in google "c# kill excel process". your article appear on the 1st.

Anonymous said...

what abt when multiple excels object are exist.. this will kill another excel object

xiaoyu on June 4, 2011 at 10:38 PM said...

You can identify the process that you want to kill based on its id. Process id is unique.

Anonymous said...

Thank you very much, only with thi code my application work!!!

Anonymous said...

Thank you thank you thank you! I've been struggling / working around this for years. This is the only code which has worked consistantly for me!

Suresh said...

Thanks a lot .. It helped me to remove excel process which we are unable to remove with any other methods.

Work Before - Working with code stuff!... on October 26, 2012 at 4:34 PM said...

Excellent machi...

Work Before - Working with code stuff!... on October 26, 2012 at 4:35 PM said...

Excellent Machi... Thanks a lot...

Anonymous said...

Very Nice, Thanks!!

Arjun on October 1, 2013 at 2:01 AM said...

Brilliant solution

Arjun on October 1, 2013 at 2:01 AM said...

Brilliant solution

Anonymous said...

Thank you for sharing! God bless you! ^_^

pep said...

Thanks! finally I can go home!!

Anonymous said...

500 websites on the topic, and this one finally has a solution that works.
Great job! Made my day!

Unknown on October 29, 2014 at 1:30 AM said...

Still works like a gem. Thanks.

Anonymous said...

YES!!! A SOLUTION THAT WORKS!

Anonymous said...

After much searching I found the solution.
Good logic.

Anonymous said...

Thanks for your solution.
God Bless

Anonymous said...

first thank you was 2008. Today is 11.6.2015; still a problem with C# 6, VS 2015, Excel 2016. This code helps me.

Anonymous said...

It worked for me..

Anonymous said...

A SOLUTION THAT WORKS FOR ME

Unknown on January 30, 2017 at 10:35 PM said...

I never write any comments when I find code for my issues, but this time I just have to say.
THANK YOU!!!!!!!!!!!

 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
Sign up for PayPal and start accepting credit card payments instantly. http://www.emailcashpro.com
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template