c# 杀掉excel进程

1122hailey 2008-12-12 03:56:00
在Winform中,运用using Excel = Microsoft.Office.Interop.Excel;引用EXCEL,完了发现进程中多了好多excel的进程,找了一些资料,说把跟EXCEL有关的全部做以下处理,就把以下代码写到了winform 的formClosing事件里,可是还是出现EXCEL的进程 ,在以下代码做了断电,好像不执行,不知道哪里的问题,请大家帮忙~

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ep);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ew);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);

excel = null;
ep = null;
range = null;
ew = null;
mySheet = null;
worksheets = null;
workbooks = null;
workbook = null;
sheet = null;
GC.Collect();
...全文
734 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mzy939216 2009-12-14
  • 打赏
  • 举报
回复
顶一个,虽然太麻烦了
j45kp 2008-12-12
  • 打赏
  • 举报
回复
excel.ActiveWorkbook.SaveAs(strExcelFileName + "\\" + timeFlag + ".xls", Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
workBook.Close(false, strExcelFileName, Type.Missing);
excel.Quit();
excel = null;

GC.Collect();//垃圾回收

Process[] myProcesses;
DateTime startTime;
myProcesses = Process.GetProcessesByName("Excel");
//得不到Excel进程ID,暂时只能判断进程启动时间
foreach (Process myProcess in myProcesses)
{
startTime = myProcess.StartTime;

if (startTime < beforeTime && startTime < afterTime)
{
myProcess.Kill();
}
}
enihs 2008-12-12
  • 打赏
  • 举报
回复
这个问题可以按照下面所说基本解决
1.停止使用某个对象时,应使用 System.Runtime.InteropServices.Marshal.ReleaseComObject。这样可以减少 RCW 的引用计数。
2。要释放对变量的引用,请将变量设置为等于 Nothing 或 Null。
3.使用 Office 应用程序对象的 Quit 方法通知服务器关闭。

关于第二点做点解释,比如如果你通过wookbooks add了个workbook,你得
books= workbooks;
book = workbooks.Add();
这样释放的时候你才可以方面的把books和book都置为null.不然直接book = Excel.Workbooks.Add();这个workbooks是没办法置为null的了。

如果通过上面的内存中还有残留,就是所谓的"僵尸程序了",是RCW造孽,不好整了,不过没什么影响
极地_雪狼 2008-12-12
  • 打赏
  • 举报
回复
if (workbook != null)
workbook.Close(true, _path + "/" + _filename, m_oMissing);

// Quit Excel and clean up.
if (worksheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
if (workbook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
if (workbook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
if (App != null)
{
App.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(App);
int iProcessID = 0;
String sProcessName = "";
System.Diagnostics.Process[] pTemp;
pTemp = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process pTempProcess in pTemp)
{
sProcessName = pTempProcess.ProcessName;
iProcessID = pTempProcess.Id;
//将EXCEL改成你需要检测的程序名,不要加exe
if (sProcessName.CompareTo("EXCEL") == 0)
{
pTempProcess.Kill();
pTempProcess.Close();
// break;
}
}
App = null;
}
cpio 2008-12-12
  • 打赏
  • 举报
回复

我试过了,app的Quit方法就可以退出

不过,如果修改了内容,会弹出对话框,提示是否保存。

可以设置app不显示Alter
wjq 2008-12-12
  • 打赏
  • 举报
回复
除了以上的
workbook调用close
app调用Quit

再不行就强行关进程吧
cpio 2008-12-12
  • 打赏
  • 举报
回复
调用excel.Close或者Quit应该就退出了吧

怎么那么麻烦

110,534

社区成员

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

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

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