杀死进程 Excel C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9316141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:51:48  来源:igfitidea点击:

Kill Process Excel C#

c#processkill

提问by Eriksson

I have to 2 process excel. For example:

我必须 2 处理 excel。例如:

1) example1.xlsx 2) example2.xlsx

1) 示例 1.xlsx 2) 示例 2.xlsx

How to kill first "example1.xlsx"?

如何先杀死“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.

那杀了一个。我只想杀一个……谢谢。

采纳答案by Ta01

The ProcessMainWindow Title will do it for you, it appends "Microsoft Excel - " to the name of the file:

ProcessMainWindow 标题将为您完成,它将“Microsoft Excel -”附加到文件名:

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.

编辑:经过测试和验证可以工作。

回答by Ani

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

Excel 将永远是一个单一的进程,AFAIK。相同的进程/窗口在其中打开多个文档。您要做的是使用 Excel 自动化来关闭您想要的文档。也许这会让你开始。http://support.microsoft.com/kb/302084

Hope this helps.

希望这可以帮助。

回答by raveturned

If your current code is working, this amendment should kill the first process it finds with the name "EXCEL".

如果您当前的代码正在运行,则此修改应终止它找到的名为“EXCEL”的第一个进程。

foreach (Process clsProcess in Process.GetProcesses())
{
  if (clsProcess.ProcessName.Equals("EXCEL"))
  {
    clsProcess.Kill();
    break;
  }
}

If you want to kill a specificprocess, you're going to have to give a bit more information.

如果你想杀死一个特定的进程,你将不得不提供更多的信息。

回答by Giedrius

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#?

您需要检查由进程打开的文件句柄,然后将其杀死。
如何检查进程持有哪个文件句柄:如何在 C# 中按进程获取打开的文件句柄列表?

foreach (Process clsProcess in Process.GetProcesses())
{
    if (clsProcess.ProcessName.Equals("EXCEL") && HasFileHandle(fileName, clsProcess))
    {
       clsProcess.Kill();
       break;
    }
 }

回答by Fiacc

Try getting the main window title

尝试获取主窗口标题

   foreach (Process clsProcess in Process.GetProcesses())
   {
      if (clsProcess.ProcessName.Equals("EXCEL")&& clsProcess.MainWindowTitle =="example")  
      {
          clsProcess.CloseMainWindow();
          break;
      }
   }

回答by JXITC

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.

刚刚在 Google 上进行了快速搜索,尝试Process.MainWindowTitle()获取 Excel 进程的标题,然后决定要杀死哪个进程。

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

我不确定这种方法,但希望这会有所帮助:

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

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

回答by user3255770

Use below logic to prevent Zombie Excel processes in Task Manager

使用以下逻辑来防止任务管理器中的 Zombie Excel 进程

 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);

回答by monkSinha

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 { }
         }
     }

回答by Kezzla

kd7'spost is an awesome answer and works well, just two things to add,

kd7 的帖子是一个很棒的答案并且效果很好,只需添加两件事,

MainWindowTitleformat is - "Filename.xlsx - Excel"

MainWindowTitle格式是—— "Filename.xlsx - Excel"

If your excel document is not visible then your MainWindowTitlewill be ""using the ""for MainWindowTitlewill kill all zombie excel process'.

如果您的 excel 文档不可见,那么您MainWindowTitle""使用""forMainWindowTitle将杀死所有僵尸 excel 进程'。

回答by REXXman

In the namespace section add this using statement.

在命名空间部分添加这个 using 语句。

using System.Diagnostics;

This example instantiated Excel with this:

此示例使用以下内容实例化 Excel:

_Application excel = new _Excel.Application();

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

此方法使用窗口句柄终止正确的 Excel 任务。

    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.

这没有失败。