C#=为什么Excel进程没有结束?

我有以下代码:

private bool IsMousetrapFile(string path) { logger.Log(validateFileMessage + path); Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Excel.Workbooks workbooks = xlApp.Workbooks; Excel.Workbook xlWorkBook = workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Sheets worksheets = (Excel.Sheets)xlWorkBook.Worksheets; Excel.Worksheet ws = null; foreach (string sheet in expectedWorksheets) { try { ws = (Excel.Worksheet)worksheets.get_Item(sheet); logger.Log(validMousetrapFileMessage + path); } catch { logger.Log(validateSheetError + sheet + ": " + path); if (ws != null) Marshal.ReleaseComObject(ws); Marshal.ReleaseComObject(worksheets); Marshal.ReleaseComObject(xlWorkBook); Marshal.ReleaseComObject(workbooks); Marshal.ReleaseComObject(xlApp); return false; } } if (ws != null) Marshal.ReleaseComObject(ws); Marshal.ReleaseComObject(worksheets); Marshal.ReleaseComObject(xlWorkBook); Marshal.ReleaseComObject(workbooks); Marshal.ReleaseComObject(xlApp); return true; } 

实际上,它会检查Excel工作簿是否包含特定工作表。 无论是否存在,我都希望Excel流程结束。 但是,每次打开新工作簿时,都会添加一个新进程,并且永远不会删除?

PS。 我知道那里有重复的代码….它应该很快整理:)

完成处理或正在执行的任何操作后,请使用Excel.Application.Quit()

在你的情况下: xlApp.Quit();

更新:

@Lasse V. Karlsen指出如果Excel已经运行该怎么办。 好吧,这是一个解决方案:(我没有测试代码,这只是为了给你一个想法)

 private void TestMethod() { bool excelWasRunning = System.Diagnostics.Process.GetProcessesByName("excel").Length > 0; // your code if (!excelWasRunning) { xlApp.Quit(); } } 

我的解决方案

 [DllImport("user32.dll")] static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId); private void GenerateExcel() { var excel = new Microsoft.Office.Interop.Excel.Application(); int id; // Find the Process Id GetWindowThreadProcessId(excel.Hwnd, out id); Process excelProcess = Process.GetProcessById(id); try { // Your code } finally { excel.Quit(); // Kill him ! excelProcess.Kill(); } 

几天前我实施了出口/import。 当我在谷歌搜索时,我从某个地方得到了以下代码,它运行正常。

 ExportToExcel() { try { //your code } catch (Exception ex) { } finally { TryQuitExcel(Application_object); } } private static void TryQuitExcel(Microsoft.Office.Interop.Excel.Application application) { try { if (application != null && application.Workbooks != null && application.Workbooks.Count < 2) { application.DisplayAlerts = false; application.Quit(); Kill(application.Hwnd); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(application); application = null; } } catch { /// Excel may have been closed via Windows Task Manager. /// Skip the close. } } private static void Kill(int hwnd) { int excelPID = 0; int handle = hwnd; GetWindowThreadProcessId(handle, ref excelPID); Process process = null; try { process = Process.GetProcessById(excelPID); // // If we found a matching Excel proceess with no main window // associated main window, kill it. // if (process != null) { if (process.ProcessName.ToUpper() == "EXCEL" && !process.HasExited) process.Kill(); } } catch { } }