找出相应进程并结束,而不是结束所有Excel进程:
public class KillExcelProcess
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static bool Kill(Microsoft.Office.Interop.Excel.Application excel)
{
try
{
IntPtr t = new IntPtr(excel.Hwnd); //得到Excel的句柄
int tag = 0;
GetWindowThreadProcessId(t, out tag); //获取本进程id
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(tag); //获取此进程的引用
p.Kill(); //关闭进程,世界真美好
return true;
}
catch (Exception ex)
{
return false;
}
}
}
|