杀死Excel 进程的方法 请高手解释代码

nikita007 2006-06-02 10:26:35
我在网上找到了一种杀死Excel 进程的方法,如下:
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();
说要写到finally中,我写了,可是编译时说worksheets 什么的都不存在,我想可能是需要先定义,可是我不知道具体的意思,请高手帮我解释一下具体操作的方法以及代码的意思。
...全文
680 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lilindiudiu 2006-08-08
  • 打赏
  • 举报
回复
mark
bhwhy 2006-06-02
  • 打赏
  • 举报
回复
这不是进程的问题.
参照这个看一下
http://community.csdn.net/Expert/topic/4788/4788931.xml?temp=4.803103E-02
David_Jin 2006-06-02
  • 打赏
  • 举报
回复
用Process.Kill方法肯定不行,因为这样会将正在应用的进程也杀掉,容易出现异常。你报错是因为虽然你定义了,但没有初始化,当然不行。

你要加保护:
myApp=new Excel.ApplicationClass();
myApp.Visible = false;
oMissiong = System.Reflection.Missing.Value;
myBooks = myApp.Workbooks;
myBook = myBooks.Open(ExcelFile,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
mySheet=(Excel.Worksheet)myBook.Sheets[1];
..........................
.........................
if(myBook != null)
myBook.Close(true, ExcelFile, oMissiong);

// Quit Excel and clean up.
if(mySheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (mySheet);
mySheet = null;
if(myBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (myBook);
myBook = null;
if(myBooks != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (myBooks);
myBooks = null;
if(myApp != null)
{
myApp.Workbooks.Close();
myApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (myApp);
myApp = null;
}
GC.Collect();
}
Knight94 2006-06-02
  • 打赏
  • 举报
回复
reference:
http://www.codeproject.com/csharp/package.asp
nikita007 2006-06-02
  • 打赏
  • 举报
回复
能给出具体的例子吗?
nikita007 2006-06-02
  • 打赏
  • 举报
回复
那么请问我应该如何写我的kill excel.dll进程的代码?
Knight94 2006-06-02
  • 打赏
  • 举报
回复
在WinApp中打开Excel,然后释放其对象并不能完全关闭,需要用Process.Kill来强制关闭。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧