c# 關閉excel程序
阿新 • • 發佈:2019-01-23
excel的application app,直接使用app.Quit();app=null;有時候不能完全關閉excel程序,而通過下面的Kill方法即Kill(app);則可以讓正在處理的excel從程序中消失。
引用名稱空間using System.Runtime.InteropServices;
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static void Kill(Microsoft.Office.Interop.Excel.Application excel)
{
IntPtr t = new IntPtr(excel.Hwnd); //得到這個控制代碼,具體作用是得到這塊記憶體入口
int k = 0;
GetWindowThreadProcessId(t, out k); //得到本程序唯一標誌k
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //得到對程序k的引用
p.Kill(); //關閉程序k
}