终止 Excel 进程 C#

发布于 2025-01-06 06:55:45 字数 327 浏览 0 评论 0原文

我必须处理 2 个 Excel。例如:

1) example1.xlsx 2) example2.xlsx

如何杀死第一个“example1.xlsx”?

我使用这段代码:

   foreach (Process clsProcess in Process.GetProcesses())
     if (clsProcess.ProcessName.Equals("EXCEL"))  //Process Excel?
          clsProcess.Kill();

即杀死两者。 我只想杀一个... 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

江湖彼岸 2025-01-13 06:55:46

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

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

如果您的 Excel 文档不可见,则您的 MainWindowTitle 将为 ""
MainWindowTitle 使用 "" 将杀死所有僵尸 excel 进程。

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

街道布景 2025-01-13 06:55:46

如果您当前的代码可以正常工作,则此修改应该杀死它找到的第一个名为“EXCEL”的进程。

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

如果您想终止特定进程,则必须提供更多信息。

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.

不离久伴 2025-01-13 06:55:46

复制并粘贴此内容。完成了!

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

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 { }
         }
     }
温柔戏命师 2025-01-13 06:55:46

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

希望这会有所帮助。

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.

深爱成瘾 2025-01-13 06:55:46

使用以下逻辑来防止任务管理器中的 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);

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);
紫罗兰の梦幻 2025-01-13 06:55:46

您需要检查进程打开的文件句柄,然后终止它。
如何检查哪个文件句柄进程正在持有: 如何在 C# 中获取进程打开的文件句柄列表?

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

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;
    }
 }
笨死的猪 2025-01-13 06:55:46

尝试获取主窗口标题

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

Try getting the main window title

   foreach (Process clsProcess in Process.GetProcesses())
   {
      if (clsProcess.ProcessName.Equals("EXCEL")&& clsProcess.MainWindowTitle =="example")  
      {
          clsProcess.CloseMainWindow();
          break;
      }
   }
风追烟花雨 2025-01-13 06:55:46

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

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

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

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

一绘本一梦想 2025-01-13 06:55:46

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

using System.Diagnostics;

此示例使用以下方式实例化 Excel:

_Application excel = new _Excel.Application();

此方法通过使用窗口句柄终止正确的 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();
            }
        }
    }

这确实有效。

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.

≈。彩虹 2025-01-13 06:55:45

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

所以本质上(快速代码):

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

使用:

KillSpecificExcelFileProcess("example1.xlsx");

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文