【问题标题】:c# excel does not clean up after creating pdf filec#创建pdf文件后excel不清理
【发布时间】:2017-09-04 13:49:21
【问题描述】:

我已使用以下代码成功地将 Excel 文件转换为 pdf 文件。
我尝试使用以下方法释放 excel 对象。
- 退出 & 关闭 & null
- 切勿对 COM 对象使用两点
- ReleaseComObject
- GC.收集

但是,在任务管理器中仍然是“EXCEL.EXE”。
我不想通过在任务管理器中调用进程列表来终止“EXCEL.EXE”

我该如何解决这个问题?

public bool ExportWorkbookToPdf(string workbookPath, string outputPath)
{
    // If either required string is null or empty, stop and bail out
    if (string.IsNullOrEmpty(workbookPath) || string.IsNullOrEmpty(outputPath))
    {
        return false;
    }

    // Create COM Objects
    Microsoft.Office.Interop.Excel.Application excelApplication;
    Microsoft.Office.Interop.Excel.Workbooks excelWorkbooks;
    Microsoft.Office.Interop.Excel.Workbook excelWorkbook;

    // Create new instance of Excel
    //var excelApplication = new Microsoft.Office.Interop.Excel.Application();
    excelApplication = new Microsoft.Office.Interop.Excel.Application();

    // Make the process invisible to the user
    excelApplication.ScreenUpdating = false;

    // Make the process silent
    excelApplication.DisplayAlerts = false;

    // Open the workbook that you wish to export to PDF
    excelWorkbooks = excelApplication.Workbooks;
    excelWorkbook = excelWorkbooks.Open(workbookPath);

    // If the workbook failed to open, stop, clean up, and bail out
    if (excelWorkbook == null)
    {
        //excelApplication.Application.Quit();
        excelApplication.Quit();

        excelWorkbook = null;
        excelWorkbooks = null;
        excelApplication = null;

        return false;
    }

    var exportSuccessful = true;
    try
    {
        excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
    }
    catch (System.Exception ex)
    {
        // Mark the export as failed for the return value...
        exportSuccessful = false;

        // Do something with any exceptions here, if you wish...
        // MessageBox.Show...        
    }
    finally
    {
        // Close the workbook, quit the Excel, and clean up regardless of the results...
        excelWorkbook.Close();
        excelWorkbooks.Close();
        excelApplication.Quit();

        excelWorkbook = null;
        excelWorkbooks = null;
        excelApplication = null;

        ReleaseExcelObject(excelWorkbook);
        ReleaseExcelObject(excelWorkbooks);
        ReleaseExcelObject(excelApplication);
    }
    return exportSuccessful;
}
private static void ReleaseExcelObject(object obj)
{
    try
    {
        if (obj != null)
        {
            Marshal.ReleaseComObject(obj);
            obj = null;
        }
    }
    catch (Exception ex)
    {
        obj = null;
        throw ex;
    }
    finally
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

【问题讨论】:

    标签: c# excel pdf excel-interop


    【解决方案1】:

    在您的 finally 语句中,您将 excelWorkbook、excelWorkbooks 和 excelApplication 设置为 null,然后再使用相同的引用传递给 ReleaseExcelObject - 因此您实际上是在调用 ReleaseExcelObject(null);除了调用 GCCollect 之外什么都不做。换个顺序试试。

    此外,在代码的前面部分 - 如果您无法打开工作簿,您只需退出应用程序并将引用设置为 null。您可以尝试将工作簿的打开放在 try 语句中,这样错误情况将在 finally 语句中被清除,而不是需要重复的代码。

    类似于:

    var exportSuccessful = true;
    try
    {
      excelWorkbook = excelWorkbooks.Open(workbookPath);
    
      // If the workbook failed to open, stop, clean up, and bail out
      if (excelWorkbook == null)
        return false;
    
        excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
    }
    catch (System.Exception ex)
    {
        // Mark the export as failed for the return value...
        exportSuccessful = false;
    
        // Do something with any exceptions here, if you wish...
        // MessageBox.Show...        
    }
    finally
    {
        // Close the workbook, quit the Excel, and clean up regardless of the results...
        excelWorkbook?.Close();
        excelWorkbooks?.Close();
        excelApplication?.Quit();
    
        ReleaseExcelObject(excelWorkbook);
        ReleaseExcelObject(excelWorkbooks);
        ReleaseExcelObject(excelApplication);
    
        excelWorkbook = null;
        excelWorkbooks = null;
        excelApplication = null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2013-08-10
      • 2017-01-23
      相关资源
      最近更新 更多