创建pdf文件后,c#excel不会清理

c# excel does not clean up after creating pdf file

我已使用下面的代码成功地将Excel文件转换为PDF文件。我尝试使用以下方法释放Excel对象。-退出并关闭空值-不要对COM对象使用两个点-释放对象气相色谱收集

但是,任务管理器中仍然保留"excel.exe"。我不想通过调用任务管理器中的进程列表来终止"excel.exe"

我该如何解决这个问题?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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();
    }
}

在finally语句中,在使用相同的引用传递给ReleaseExcelObject之前,将ExcelWorkbooks&ExcelApplication设置为空,因此您实际上是在调用ReleaseExcelObject(空);它只调用gcCollect。尝试交换订单。

另外,在代码的前面-如果您无法打开工作簿,只需退出应用程序并将引用设置为空。您可以尝试在try语句中打开工作簿,这样就可以在finally语句中清除错误条件,而不需要重复的代码。

沿着这条线的东西:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
}