System.Runtime.InteropServices.Marshal.ReleaseComObject(wSheet);
wBook.Close(true, strFilePath, System.Reflection.Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wBooks);
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
我做了以上步骤,但发现进程里面excel有N个 excel.exe!
遍历进程
然后
Process.kill
{
System.Diagnostics.Process[] allProcess = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process thisprocess in allProcess)
{
string processName = thisprocess.ProcessName;
if (processName.ToLower() == "excel")
{
try
{
thisprocess.Kill();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return;
}
}
}
}
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
if (xlapp != null)
{
xlapp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp);
foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
//先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,
//但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它:p
if (theProc.CloseMainWindow() == false)
{
theProc.Kill();
}
}
xlapp = null;
}
{ GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{ Man.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } }