日期:2014-05-17 浏览次数:21036 次
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(); } } }
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 } } }