1

I open and read data from excel by below flow:

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();

But after above process, i check in Task Manager, EXCEL.EXE process still alive, don't be killed. Please tell me why, help me a solution!!!

mybirthname
  • 17,539
  • 3
  • 30
  • 53
hungbm06
  • 1,541
  • 5
  • 23
  • 32

1 Answers1

0

How about this code

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

After all your code and when you think there is no need of excel file write this code

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();
Developer
  • 8,228
  • 37
  • 122
  • 230