2011-03-09 19 views
1

我打開和閱讀由下面流從Excel數據:不能殺死EXCEL.EXE進程和讀取Excel文件

ApplicationClass objApp = new ApplicationClass(); 
Workbooks objBooks = objApp.Workbooks; 
Workbook objBook = objBooks.Open(..) 
// Do something (read data...) 
... 
objBook.Close(false, Missing.Value, Missing.Value); 
objApp.Quit(); 
Marshal.ReleaseComObject(objBooks); 
Marshal.ReleaseComObject(objBook); 
Marshal.ReleaseComObject(objApp); 
objBook = null; 
objApp = null; 
GC.Collect(); 
GC.WaitForPendingFinalizers(); 

但上述過程後,我在任務管理器中查看,EXCEL.EXE進程還活着,不要被打死。請告訴我爲什麼,幫我一個解決方案!

+0

應該有在ApplicationClass實例的退出方法。 – deostroll 2011-03-09 17:21:17

+0

我想念這種方法,我真的叫這種方法。的 – hungbm06 2011-03-09 17:23:48

+0

可能重複[如何正確地清理在C#中的Excel互操作對象(http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c) – David 2011-03-09 17:25:28

回答

0

這個怎麼樣代碼

[DllImport("user32.dll")] 
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr ProcessId); 

所有的代碼後,當你覺得沒有必要excel文件的編寫代碼

IntPtr hwnd = new IntPtr(ExcelObj.Hwnd); // Your Excel Application name 
IntPtr processId; 
IntPtr foo = GetWindowThreadProcessId(hwnd, out processId); 
Process proc = Process.GetProcessById(processId.ToInt32()); 
proc.Kill();