C#.net调用Excel出现问题
excel進程無法通過excelapp.quit()退出。
1.嘗試http://support.microsoft.com/kb/317109/zh-cn中的方法,不起作用。
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
worksheets=null;
worksheet=null;
excelApp=null;
range=null;
把操作Excel文件用到的對象實例全部釋放。
然后資源回收!
GC.Collect();
2.Kill掉
oExcelApp.Quit(); ????
oExcelApp = null; ????
PublicMethod.Kill(oExcelApp);//調用kill當前excel進程
PublicMethod.Kill()內容是:
using System.Runtime.InteropServices; ?
public class PublicMethod ?
{ ??
??? public PublicMethod() ??
??? { ???// ???// TODO: 在此處添加構造函數邏輯 ???// ??} ??
[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 ??
}
}
轉載于:https://www.cnblogs.com/claire2013/p/3238938.html
總結
以上是生活随笔為你收集整理的C#.net调用Excel出现问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刺客信条泉水护卫稀有怎么打
- 下一篇: java--模板方法模式