技术开发 频道

ASP.NET excel导出 excel.exe进程关闭

 【IT168技术文档】 思路: 最先获取进程里的所有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();
 

查看原文地址

0
相关文章