C# 調用excel後,殺掉excel進程

//轉載自: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));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章