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:
thank you!
thank you very much, it works excelently....:)
Thank yoo so much! Tried everything but only this works!!!
This is just a fantastic way. It solved my application from crashing. Thanks a lot and a lot.
Thanks a million!! Works excellently!!
Feels like cheating... but i love it anyway.
Its wonderful...worked out perfectly...
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.
Thanks. It works perfectly in my web application. =)
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.
Thank you very much. With this code i can kill any process from task manager not just excel. Thanks a lot.
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!
Thanks a lot. I was searching for the solution to get rid of excel instances in task manager. This surely helps.
Hi, Thanks for the sample.
How to Kill Specific process/excel file?
Thanks
Hi, you can based on the ExcelProcess.Id to kill a specific process.
yeah. you rock man. thanks you for sharing. just search in google "c# kill excel process". your article appear on the 1st.
what abt when multiple excels object are exist.. this will kill another excel object
You can identify the process that you want to kill based on its id. Process id is unique.
Thank you very much, only with thi code my application work!!!
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!
Thanks a lot .. It helped me to remove excel process which we are unable to remove with any other methods.
Excellent machi...
Excellent Machi... Thanks a lot...
Very Nice, Thanks!!
Brilliant solution
Brilliant solution
Thank you for sharing! God bless you! ^_^
Thanks! finally I can go home!!
500 websites on the topic, and this one finally has a solution that works.
Great job! Made my day!
Still works like a gem. Thanks.
YES!!! A SOLUTION THAT WORKS!
After much searching I found the solution.
Good logic.
Thanks for your solution.
God Bless
first thank you was 2008. Today is 11.6.2015; still a problem with C# 6, VS 2015, Excel 2016. This code helps me.
It worked for me..
A SOLUTION THAT WORKS FOR ME
I never write any comments when I find code for my issues, but this time I just have to say.
THANK YOU!!!!!!!!!!!
Post a Comment