终止 Excel 进程 C#
我必须处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
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
""
forMainWindowTitle
will kill all zombie excel process'.如果您当前的代码可以正常工作,则此修改应该杀死它找到的第一个名为“EXCEL”的进程。
如果您想终止特定进程,则必须提供更多信息。
If your current code is working, this amendment should kill the first process it finds with the name "EXCEL".
If you want to kill a specific process, you're going to have to give a bit more information.
复制并粘贴此内容。完成了!
Copy and paste this. Its done!
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.
使用以下逻辑来防止任务管理器中的 Zombie Excel 进程
Use below logic to prevent Zombie Excel processes in Task Manager
您需要检查进程打开的文件句柄,然后终止它。
如何检查哪个文件句柄进程正在持有: 如何在 C# 中获取进程打开的文件句柄列表?
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#?
尝试获取主窗口标题
Try getting the main window title
刚刚在 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
在命名空间部分添加此 using 语句。
此示例使用以下方式实例化 Excel:
此方法通过使用窗口句柄终止正确的 Excel 任务。
这确实有效。
In the namespace section add this using statement.
This example instantiated Excel with this:
This method kills the right Excel task by using the window handle.
This worked without fail.
ProcessMainWindow 标题将为您完成此操作,它将“Microsoft Excel - ”附加到文件名:
所以本质上(快速代码):
使用:
编辑:经过测试和验证可以工作。
The ProcessMainWindow Title will do it for you, it appends "Microsoft Excel - " to the name of the file:
So essentially (quick code):
Use:
Edit: Tested and verified to work.