在ASP.NET中调用EXCEL后(比如导出EXCEL),EXCEL长驻内存的解决办法就是---干掉它!!!
原帖地址: http://hi.baidu.com/cnzynet/blog/item/b7d1a0f22dc00315b17ec50f.html
添加引用:
using System.Runtime.InteropServices;
结束进程代码
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);
public void KillExcellProcess(string path)
{
Excel.ApplicationClass excel = new Excel.ApplicationClass();//.applicationclass();
excel.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);
public void KillExcellProcess(string path)
{
Excel.ApplicationClass excel = new Excel.ApplicationClass();//.applicationclass();
excel.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}