阅读背景:

C#导出Excel后无法结束相应进程的解决办法

来源:互联网 
找出相应进程并结束,而不是结束所有Excel进程:

public class KillExcelProcess
{
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

    public static bool Kill(Microsoft.Office.Interop.Excel.Application excel)
    {
        try
        {
            IntPtr t = new IntPtr(excel.Hwnd);   //得到Excel的句柄
            int tag = 0;
            GetWindowThreadProcessId(t, out tag);   //获取本进程id
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(tag);   //获取此进程的引用
            p.Kill();     //关闭进程,世界真美好
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}
public class 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: