Application.Hwnd has a valid window handle that you can use to get the process for the associated Excel application, even if the application does not have a visible workbook window.
[DllImport("user32.dll")] static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId); Process GetExcelProcess(Microsoft.Office.Interop.Excel.Application excelApp) { int id; GetWindowThreadProcessId(excelApp.Hwnd, out id); return Process.GetProcessById(id); } void TerminateExcelProcess(Microsoft.Office.Interop.Excel.Application excelApp) { var process = GetExcelProcess(excelApp); if (process != null) { process.Kill(); } } private void button1_Click(object sender, EventArgs e) {
Note. There are several answers on this question explaining why the Excel process will not end when you finish working with it with C # through automation (if you are not very pedantic that you release every link to any object that you use explicitly).
NineBerry Jun 05 '16 at 12:44 2016-06-05 12:44
source share