C#操作Excel,关闭EXCEL进程
使用C#操作Excel的COM组件的过程中,最后把EXCEL进程关闭掉是十分重要的。
首先第一种是利用.net的GC,即垃圾回收器。代码如下:
using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices; 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; System.Runtime.InteropServices.Marshal.ReleaseComObject(wSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(wBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); GC.Collect(); //显式调用GC Console.Read(); } }}
关注后面四行,System.Runtime.InteropServices.Marshal类主要提供对非受托管内存的操作。对于函数ReleaseComObject,在MSDN上的解释是“该运行库可调用包装具有引用计数,每次将 COM 接口指针映射到该运行库可调用包装时,此引用计数都将递增。ReleaseComObject 方法递减运行库可调用包装的引用计数。当引用计数达到零时,运行库将释放非托管 COM 对象上的所有引用。”即断开对对象的引用,这样GC发现对象不会再被使用,那么就会回收它。
第二种方法是通过进程的ID号把进程kill掉。但第一种方法有网友说有时候不灵,因为本人只是简单地用控制台来测试,可能在其它情况下不一定行,所以推荐使用第二种方法。
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 } }}
GetWindowThreadProcessId函数是User32.dll中函数,作用是通过对象的句柄得到对象的进程ID。extern 修饰符用于声明在外部实现的方法。明白这点后,整个过程就很简单了:其实就是先获得Excel.Application实例app的句柄,再通过句柄得到进程ID,最后把该ID的进程Kill掉。