可爱的琪琪,开心快乐每一天
温馨小屋
可爱的琪琪,开心快乐每一天
posts - 7,comments - 39,trackbacks - 0

1。网上的一种说法,关闭range,关闭worksheet,关闭workbook,最后关闭excel
System.Runtime.InteropServices.Marshal.ReleaseComObject(Range);
2System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
3System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);

Range = null;
myBook = null;
myExcel = null;

我试过,没有成功


2。关闭进程
C#和Asp.net下excel进程一被打开,有时就无法关闭,   尤其是website.对关闭该进程有过GC、release等方法,但这些方法并不是在所有情况下均适用。  于是提出了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

posted on 2006-11-04 11:28 陈琪 阅读(10103) 评论(16)  编辑  收藏 所属分类: C#

FeedBack:
# re: c#操作excel后关闭excel.exe的方法
2007-03-19 15:55 | kennba
以上代码如果用在后期绑定时,
应该怎么改呀?  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2007-03-19 15:58 | kennba
object objApp_Late;

Type objClassType;
objClassType = Type.GetTypeFromProgID("Excel.Application");
objApp_Late = Activator.CreateInstance(objClassType);

IntPtr t = new IntPtr(objApp_Late.Hwnd);

这么写不对,应该怎么办呢?

  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2007-04-05 11:12 | 陈琪
主要是要找到excel进程对应的进程号,然后通过
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
把进程杀死。
你上面所写的代码中并没有打开excel文件吧  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2007-04-13 11:37 | eddy
非常感激,好好用  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2007-04-15 18:05 | tank414
@eddy
谢谢兄弟夸奖,嘿嘿  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2007-05-23 12:32 | 养徐徐
谢谢关掉了  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法[未登录]
2007-09-24 18:41 | Jet
怎么我那么做 k老是等于0啊  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法[未登录]
2008-01-18 17:14 |
我也是K一直为0....是ASP.NET....请求帮助啊..知道的请发我邮箱
o_ogrowth@hotmail.com
谢谢了。  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2008-07-04 14:45 | 陈云
thanks  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2008-08-02 10:37 | 一个人喝醉
方法是很好,可发布后就不能关掉了,好像要设置什么权限吧  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2009-02-11 15:27 | 孤城
不错不错,我收藏了~~  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2009-08-07 15:32 | 陈卓
用了你的方法怎么在我出现
无法再DLL“user32.dll”找到名为“GetWindowThreadProcessId”的入口点  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2009-08-31 14:19 | 老王
写的太好了 非常感谢 呵呵  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2009-10-30 15:44 | 关闭excel
一发布就用不了,拒绝访问!!!不知道是不是权限问题,谁解决了?  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法
2009-11-30 15:47 | nbbnmm
谢谢,真的搞定了。  回复  更多评论
  
# re: c#操作excel后关闭excel.exe的方法[未登录]
2011-07-15 08:27 | liu
试试哈,谢谢了啊  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: