导出excel后关闭excel进程

[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

worksheet.SaveAs(filePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault);
            book.Close(true, null, null);          

            System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
            excel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

            if (excel != null)
            {
                try
                {
                    int lpdwProcessId;
                    GetWindowThreadProcessId(new IntPtr(excel.Hwnd), out lpdwProcessId);
                    System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
                }
                catch (Exception ex1)
                {
                }
            }

            worksheet = null;
            book = null;
            excel = null;
            
            GC.Collect();            

 

posted on 2017-01-19 16:02  数据结构X  阅读(772)  评论(0编辑  收藏  举报

导航