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,949
  • 3
  • 31
  • 55
hungbm06
  • 1,549
  • 6
  • 24
  • 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,390
  • 41
  • 129
  • 238