导出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();            
原文地址:https://www.cnblogs.com/ToddLai/p/6307004.html