日期:2014-05-17  浏览次数:20442 次

excel进程不能关闭
    服务器读取完excel后想把excel进程关闭,试了下面的方法都不行
if (excelApp != null)
                {
                    //Marshal.ReleaseComObject(excelApp);                    
                    excelApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                    //KillExcel(excelApp);
                    excelApp = null;
                }    
    ReleaseComObject不能关闭进程
    
private static void KillExcel(Microsoft.Office.Interop.Excel.Application theApp)
        {
            int id = 0;
            IntPtr intptr = new IntPtr(theApp.Hwnd);
            System.Diagnostics.Process p = null;
            try
            {
                GetWindowThreadProcessId(intptr, out id);
                p = System.Diagnostics.Process.GetProcessById(id);
                if (p != null)
                {
                    p.Kill();
                    p.WaitForExit();
                    p.Dispose();
                }
            }
            catch (Exception ex)
            {

            }
        }
  p.Kill();会报拒绝访问错误