Application.Hwnd
has a valid window handle that you can use to get the process for the associated Excel application even when the application has no 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)
{
// Create Instance of Excel
Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
try
{
// Work with oXL
}
finally
{
TerminateExcelProcess(oXL);
}
}
Note: This question has some answers that explain why the Excel process will not terminate when you are finished working with it from C# via automation (unless you are very pedantic about making sure you release every reference to any object you use explicitly).