打开并读取excel文件后无法杀死EXCEL.EXE进程

时间:2011-03-09 17:18:59

标签: c# excel com process

我打开并通过以下流程从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进程还活着,不要被杀死。请告诉我原因,帮我解决一下!!!

1 个答案:

答案 0 :(得分: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();