C# 导出数据到Excel后,销毁Excel.exe进程的方法
上个星期做一个复杂的报表导出到excel,发现每次导出后都有一个excel.exe进程.找到N久才找到的这段代码:
#region Kill Special Excel Process
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
#endregion
Excel.Application xlApp = new Excel.Application();
.....导出excel后
if (xlApp != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(xlApp.Hwnd), out lpdwProcessId);
System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}