//轉載自:http://bbs.csdn.net/topics/390255015/ 收藏備用。
public class KillExcel
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
/// <summary>
/// 強制關閉當前Excel進程
/// </summary>
public static void Kill(IntPtr intPtr)
{
try
{
Process[] ps = Process.GetProcesses();
int ExcelID = 0;
GetWindowThreadProcessId(intPtr, out ExcelID); //得到本進程唯一標誌k
foreach (Process p in ps)
{
if (p.ProcessName.ToLower().Equals("excel"))
{
if (p.Id == ExcelID)
{
p.Kill();
}
}
}
}
catch
{
//不做任何處理
}
}
}
//調用KillExcel.Kill(new IntPtr(excelApp.Hwnd));