2012-04-01 29 views
1

爲什麼這會導致excel被打開?爲什麼openfiledialog會導致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文件,如上圖所示,我看到在任務管理器的進程。

可有人告訴我這怎麼可能?

+0

如果一切都失敗了,單步通過代碼!它應該很快顯示哪條線路導致問題。此外,Excel返回的任何對象也必須在完成時通過ReleaseComObject發佈,例如。 xlWorkBook,xlWorkSheet,xlWorkSheet.get_Range,EmailList.Cells。想念任何一個Excel將保持開放。 – Will 2012-04-01 19:45:19

+0

ps。另外,你的清理代碼應該在finally塊中,以保證它被調用,而不管成功或異常。 – Will 2012-04-01 19:48:22

+0

非常感謝。你可以請看看編輯的問題,我想我找到了原因爲什麼 – 2012-04-01 20:10:50

回答

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

非常感謝您的幫助。我的應用程序打開excel,它應該關閉它。 「excel已經打開?」是什麼意思? – 2012-04-01 19:08:52

+0

????????????????/ – 2012-04-01 19:26:42

+0

可能有一個Excel應用程序已經打開 - 可見或不可見。在後一種情況下,你會在任務管理器中看到它。 – 2012-04-01 19:27:09

相關問題