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

时间:2012-04-01 18:33:40

标签: c# .net excel excel-interop

为什么这会导致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!

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

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

1 个答案:

答案 0 :(得分:1)

如果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(); 
} 
相关问题