如何停止从 C# .Net 应用程序启动的 Excel 进程?

发布于 2024-08-22 21:34:59 字数 68 浏览 5 评论 0 原文

我正在从 C# .Net 应用程序运行 Excel,但无法关闭该进程。如何从我的 C# 程序中正确关闭 Excel 进程?

I'm running Excel from a C# .Net application and having trouble getting the process to shut down. How can I get the Excel process to shut down correctly from my C# program?

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

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

发布评论

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

评论(4

请远离我 2024-08-29 21:34:59

试试这个:

foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
        {
            if (process.MainModule.ModuleName.ToUpper().Equals("EXCEL.EXE"))
            {
                process.Kill();
                break;
            }
        }

Try this:

foreach (System.Diagnostics.Process process in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
        {
            if (process.MainModule.ModuleName.ToUpper().Equals("EXCEL.EXE"))
            {
                process.Kill();
                break;
            }
        }
忘羡 2024-08-29 21:34:59

我大约 99% 确定您的问题是由悬挂的 COM 引用阻止 Excel 进程关闭引起的。 Excel 特别容易出现这种情况,因为它倾向于透明地生成 COM 引用,而不为您提供获取其句柄并显式关闭它们的方法。

PIA 通过创建自己的一组对底层 COM 对象的引用来使这个问题变得更加复杂,除非您显式地让它们清理,否则它们不会清理这些引用。正是由于这个原因,通过 PIA 使用 Excel 非常繁琐。

Application.Quit()(IIRC 这就是它的名字)如果引用全部被正确清理,关闭进程。这意味着您必须仔细管理保存对 Excel 进程的 COM 引用的任何内容的生命周期,并确保正确清理所有 COM 引用。

I'm about 99% certain that your problem is caused by dangling COM references preventing the Excel process from shutting down. Excel is particularly prone to this as it tends to transparently generate COM references without giving you the means to get at their handles and explicitly shut them down.

The PIAs compound this problem by making their own set of references to the underlying COM objects that they do not clean up unless you explicitly make them do it. Working with Excel through the PIAs is quite fiddly for precisely this reason.

Application.Quit() (IIRC this is what it's called) will shut down the process if the references are all correctly cleaned up. This means that you have to carefully manage the life cycle of anything that holds COM references to the Excel process and make sure that all COM references are cleaned up properly.

雪若未夕 2024-08-29 21:34:59
Application app = new Application();
...
EndTask((IntPtr)app.Hwnd, true, true);

[DllImport("user32.dll")]
public static extern bool EndTask(IntPtr hwnd, bool shutdown, bool force);
Application app = new Application();
...
EndTask((IntPtr)app.Hwnd, true, true);

[DllImport("user32.dll")]
public static extern bool EndTask(IntPtr hwnd, bool shutdown, bool force);
十六岁半 2024-08-29 21:34:59

根据您的需要,您可以考虑使用 SpreadsheetGear for .NET,它为您提供了一个无需 COM 的 Excel 兼容组件Interop(无挂起实例,性能更好,不必担心是否安装了 Excel 或安装了哪个版本的 Excel)。

您可以在此处查看使用 C# 和 VB 源代码的实时 ASP.NET 示例,了解有关 SpreadsheetGear 的更多信息Windows 窗体控件和示例此处,并下载免费试用版此处

免责声明:我拥有 SpreadsheetGear LLC

Depending on your needs, you might consider using SpreadsheetGear for .NET which gives you an Excel compatible component with no COM Interop (no hanging instances, better performance, don't have to worry whether Excel is installed or which version of Excel is installed).

You can see live ASP.NET samples with C# and VB source here, learn more about SpreadsheetGear's Windows Forms controls and samples here, and download a free trial here if you want to try it yourself.

Disclaimer: I own SpreadsheetGear LLC

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