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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-18 我要评论
想了解解析c#操作excel后关闭excel.exe的方法的相关内容吗,在本文为您仔细讲解c# excel excel.exe的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c#,excel,excel.exe,下面大家一起来学习吧。
于是提出了kill process的方法,目前我见过的方法多是用进程创建时间筛选excel.exe进程,然后kill 。这样的方法是不精确的,也是不安全的,通过对网上一些关于Api运用文章的阅读,我找到了更为直接精确找到这个process并kill的方法
以下就是代码        
复制代码 代码如下:

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们