12

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
Eriksson
  • 159
  • 1
  • 2
  • 11
  • just did a quick search on Google, try Process.MainWindowTitle() to get the title of the Excel process, and decide which one is that you want to kill. – JXITC Feb 16 '12 at 17:45
  • 1
    The proper way to do this is to track and release the Application COM object and all it's dependencies. – Motomotes Dec 23 '13 at 14:21

10 Answers10

13

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.

Ta01
  • 31,040
  • 13
  • 70
  • 99
  • What 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 – Ta01 Feb 16 '12 at 19:36
  • If you have two excel document opened, and if you run this code, that close the both.... – Eriksson Feb 16 '12 at 22:09
  • I 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"??? – Eriksson Feb 16 '12 at 23:03
4

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'.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Kezzla
  • 189
  • 1
  • 8
2

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 { }
         }
     }
monkSinha
  • 341
  • 3
  • 10
2

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.

raveturned
  • 2,637
  • 24
  • 30
1

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.

Ani
  • 10,826
  • 3
  • 27
  • 46
0

Use below logic to prevent Zombie Excel processes in Task Manager

 List<int> GetAllExcelProcessID()
    {
       List<int> ProcessID = new List<int>(); 
       if (currentExcelProcessID == -1)
        {
           List<System.Diagnostics.Process> currentExcelProcessList = System.Diagnostics.Process.GetProcessesByName("EXCEL").ToList();
           foreach(var item in currentExcelProcessList)
            {
                ProcessID.Add(item.Id);
            }
        }
       return ProcessID;
    }
int GetApplicationExcelProcessID(List<int> ProcessID1, List<int> ProcessID2)
    {
        foreach(var processid in ProcessID2)
        {
            if (!ProcessID1.Contains(processid)) { currentExcelProcessID = processid; }
        }
        return currentExcelProcessID;
    }
 void KillExcel()
    {
        System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(currentExcelProcessID);
        process.Kill();
    }
 List<int> ProcessID1 = GetAllExcelProcessID();
                excel = new Excel.Application();
                List<int> ProcessID2 = GetAllExcelProcessID();
                currentExcelProcessID = GetApplicationExcelProcessID(ProcessID1, ProcessID2);
0

In the namespace section add this using statement.

using System.Diagnostics;

This example instantiated Excel with this:

_Application excel = new _Excel.Application();

This method kills the right Excel task by using the window handle.

    public void Kill()
    {
        Int32 ExcelHwnd = excel.Hwnd;
        Process[] localExcel = Process.GetProcessesByName("EXCEL");
        foreach (Process Pgm in localExcel)
        {
            // xlMinimized keeps the screen from flashing when the user interface is made 
            // visible with the excel.visible needed to set the MainWindowHandle
            excel.WindowState = XlWindowState.xlMinimized;
            excel.Visible = true;
            if ((Pgm.ProcessName == "EXCEL") && (ExcelHwnd == Pgm.MainWindowHandle.ToInt32()))
            {
                Pgm.Kill();
            }
        }
    }

This worked without fail.

REXXman
  • 376
  • 2
  • 4
0

You need to check file handles, that are opened by process and then kill it.
How to check which file handles process is holding: How do I get the list of open file handles by process in C#?

foreach (Process clsProcess in Process.GetProcesses())
{
    if (clsProcess.ProcessName.Equals("EXCEL") && HasFileHandle(fileName, clsProcess))
    {
       clsProcess.Kill();
       break;
    }
 }
Community
  • 1
  • 1
Giedrius
  • 8,430
  • 6
  • 50
  • 91
0

Try getting the main window title

   foreach (Process clsProcess in Process.GetProcesses())
   {
      if (clsProcess.ProcessName.Equals("EXCEL")&& clsProcess.MainWindowTitle =="example")  
      {
          clsProcess.CloseMainWindow();
          break;
      }
   }
Fiacc
  • 1,324
  • 1
  • 15
  • 24
0

just did a quick search on Google, try Process.MainWindowTitle() to get the title of the Excel process, and decide which one is that you want to kill.

I am not sure about this method, but hope this will help:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowtitle.aspx

JXITC
  • 1,110
  • 1
  • 13
  • 27