为什么openfiledialog导致excel被添加到任务管理器进程中

本文关键字:任务管理器 进程 添加 openfiledialog 导致 excel 为什么 | 更新日期: 2023-09-27 18:29:44

为什么这会导致excel打开?

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {
            //EXCEL.EXE *32 is now showing in the task manager!

我从openfiledialog中选择了一个XLSX文件,如上所示,我在任务管理器中看到了这个过程。

有人能告诉我这怎么可能吗?

为什么openfiledialog导致excel被添加到任务管理器进程中

如果Excel已经打开,您应该尝试获取此实例,而不是创建一个新实例。

using System.Runtime.InteropServices;
...
Excel.Application xl = null; 
try {
    // Try to get an existing instance
    xl = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); 
} catch (COMException ex) { 
    // Excel was not open. Open a new instance
    xl = new Excel.ApplicationClass(); 
}