源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#利用com操作excel释放进程的解决方法

  • 时间:2021-05-04 05:11 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#利用com操作excel释放进程的解决方法
第一个
[u]复制代码[/u] 代码如下:
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);         System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);         System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);         System.Runtime.InteropServices.Marshal.ReleaseComObject(range);         excelApp = null;         wbclass = null;         sheets = null;         worksheet = null;         range = null;         GC.Collect();         GC.WaitForPendingFinalizers();
释放不彻底,还是有进程存在。 第二种
[u]复制代码[/u] 代码如下:
//调用底层函数获取进程标示     [DllImport("User32.dll")]     public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId);     private static void KillExcel(Microsoft.Office.Interop.Excel.Application theApp)     {         int id = 0;         IntPtr intptr = new IntPtr(theApp.Hwnd);         System.Diagnostics.Process p = null;         try         {             GetWindowThreadProcessId(intptr, out id);             p = System.Diagnostics.Process.GetProcessById(id);             if (p != null)             {                 p.Kill();                 p.Dispose();             }         }         catch (Exception ex)         {         }     }
这个方法比较好,我试过了可以关闭掉进程。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部