Kill Process Excel C#
Solution 1
The ProcessMainWindow Title will do it for you, it appends "Microsoft Excel - " to the name of the file:
So essentially (quick code):
private void KillSpecificExcelFileProcess(string excelFileName)
{
var processes = from p in Process.GetProcessesByName("EXCEL")
select p;
foreach (var process in processes)
{
if (process.MainWindowTitle == "Microsoft Excel - " + excelFileName)
process.Kill();
}
}
Use:
KillSpecificExcelFileProcess("example1.xlsx");
Edit: Tested and verified to work.
Solution 2
kd7's post is an awesome answer and works well, just two things to add,
MainWindowTitle
format is - "Filename.xlsx - Excel"
If your excel document is not visible then your MainWindowTitle
will be ""
using the ""
for MainWindowTitle
will kill all zombie excel process'.
Solution 3
If your current code is working, this amendment should kill the first process it finds with the name "EXCEL".
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
If you want to kill a specific process, you're going to have to give a bit more information.
Solution 4
Excel will always be a single process, AFAIK. The same process/windows opens multiple documents inside it. What you want to do is use Excel automation to CLOSE the document you want to. Perhaps this will get you started. http://support.microsoft.com/kb/302084
Hope this helps.
Solution 5
Copy and paste this. Its done!
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (System.Diagnostics.Process p in process)
{
if (!string.IsNullOrEmpty(p.ProcessName))
{
try
{
p.Kill();
}
catch { }
}
}
Eriksson
Updated on April 18, 2020Comments
-
Eriksson about 4 years
I have to 2 process excel. For example:
1) example1.xlsx 2) example2.xlsx
How to kill first "example1.xlsx"?
I use this code:
foreach (Process clsProcess in Process.GetProcesses()) if (clsProcess.ProcessName.Equals("EXCEL")) //Process Excel? clsProcess.Kill();
That kill a both. I wanna kill just one... Thank you.
-
Eriksson about 12 yearsWhat library is defined HasFileHandle? Thank u
-
Ta01 about 12 yearsWhat do you mean it doesn't work, did you try just opening example1.xlsx and see if it closes it? Are you automating Excel with other code that is causing multiple EXCEL.exe 's to spawn
-
Eriksson about 12 yearsIf you have two excel document opened, and if you run this code, that close the both....
-
Eriksson about 12 yearsI have this: Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlBook = xlApp.Workbooks.Open("c:\\teste.xlsx", 0, false, format, null, null, false, Excel.XlPlatform.xlWindows, null, true, false, 0, true, false, false); This works... But why MainWindowTitle is equals to a empty string but not equals "Microsoft Excel - teste.xlsx"???
-
Giedrius about 12 yearsIt's a pseudomethod, you can find implementation in my mentioned stackoverflow link.
-
user3255770 over 10 yearsThis script will keept other excels which are opened before your application starts.