Kill Process Excel C#

后端 未结 10 1479
栀梦
栀梦 2020-12-09 12:45

I have to 2 process excel. For example:

1) example1.xlsx 2) example2.xlsx

How to kill first \"example1.xlsx\"?

I use this code:

   f         


        
相关标签:
10条回答
  • 2020-12-09 13:01

    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.

    0 讨论(0)
  • 2020-12-09 13:01

    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.

    0 讨论(0)
  • 2020-12-09 13:02

    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 { }
             }
         }
    
    0 讨论(0)
  • 2020-12-09 13:05

    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.

    0 讨论(0)
  • 2020-12-09 13:10

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

    0 讨论(0)
  • 2020-12-09 13:10

    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.

    0 讨论(0)
提交回复
热议问题