您的位置:首页 > 编程语言 > ASP

ASP.NET excel导出 excel.exe进程关闭 !经典写法。绝对实用。

2009-07-28 17:45 597 查看
思路: 最先获取进程里的所有Excel.exe进程 当后台实例化一个Excel。exe 后 在次获取进程里的所有Excel.exe 在做比较获取Pid 多出的那个就是 刚刚开启的。

列:

//获取Excel进程

Process[] P0, P1;

P0 = Process.GetProcessesByName("Excel");

Application xlApp = new ApplicationClass();

int I, J;

P1 = Process.GetProcessesByName("Excel");

// 比较前后 多出的Excel.exe 的pid

I = 0;

if (P1.Length > 1)
{

for (I = 0; I < P1.Length; I++)
{

for (J = 0; J < P0.Length; J++)
{

if (P0[J].Id == P1[I].Id)
{
break;
}
}

if (J == P0.Length) break;

}

}

Process P = P1[I];

//关闭进程

xlBook.Close(false, Type.Missing, Type.Missing);
xlApp.Workbooks.Close();
//xlApp.Application.Quit();
if(ws!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
ws = null;
}
if (xlBook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
xlBook = null;
}
if (xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
if (P.ToString() != "")
{
P.Kill();
}
GC.Collect();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: