C# 关闭 Excel进程


namespace ExcelTest
  {
      class DataOutput
      {
          static void Main(string[] args)
          {
              Excel.Application app = new Excel.ApplicationClass();
              Excel.Workbook wBook = app.Workbooks.Add(true);
              Excel.Worksheet wSheet = wBook.Worksheets[1] as Excel.Worksheet;
            app.Visible = true;

            Kill(app);

            Console.Read();
        }

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

        public static void Kill(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
 
         }
     }
}

posted @ 2013-10-19 18:26  梦想(胡大利)  阅读(1170)  评论(0编辑  收藏  举报