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

源码网商城

解析c#操作excel后关闭excel.exe的方法

  • 时间:2020-07-14 02:38 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:解析c#操作excel后关闭excel.exe的方法
于是提出了kill process的方法,目前我见过的方法多是用进程创建时间筛选excel.exe进程,然后kill 。这样的方法是不精确的,也是不安全的,通过对网上一些关于Api运用文章的阅读,我找到了更为直接精确找到这个process并kill的方法 [b]以下就是代码[/b]        
[u]复制代码[/u] 代码如下:
using   System.Runtime.InteropServices;     [DllImport("User32.dll",   CharSet   =   CharSet.Auto)]     public   static   extern   int   GetWindowThreadProcessId(IntPtr   hwnd,   out   int   ID);     protected   void   Button1_Click(object   sender,   EventArgs   e)     {         Excel.ApplicationClass   excel   =   new   Microsoft.Office.Interop.Excel.ApplicationClass();         excel.Workbooks.Open("d:\aaa.xls",   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();                      }
以上代码百分百成功的关闭excel.exe进程 我的做法是结合两者,先释放资源,然后关闭进程。 同时网上说避免使用GC.Collect 方法 (),因为会导致整个clr进行gc,影响你的性能.所以我也没有调用GC.Collect
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部