为什么我的功能没有正确终止excel?

时间:2017-10-20 12:18:49

标签: c# excel

有人可以告诉我,为什么我的excel流程会保持开放状态?如果defaultPrinter == null变为true,则正在运行的excel进程将按预期终止。

我已经确保,可以访问对ReleaseComObject()的最后一次调用,这意味着我可以在我的日志中看到消息Releasing COM objects

// Gets the configured default printer from the printer excel sheet 
static void GetDefaultPrinter(string printerFile, string clientname)
{
    // Create excel application object by calling constructor 
    var xlApplication = new Excel.Application();
    var xlWorkbooks = xlApplication.Workbooks;
    var xlWorkbook = xlWorkbooks.Open(Environment.GetEnvironmentVariable("TEMP") + @"\" + Environment.UserName + "@" + GetColName(clientname) + ".xls");                                              

    // Open first sheet within excel document (index start at 1, not 0) 
    Excel._Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets["Printer<->Computer_Relations"];

    // Get used sheet bounderies 
    Excel.Range xlRange = xlWorksheet.UsedRange;

    // Get row & column count 
    int rowCount = xlRange.Rows.Count;
    int colCount = xlRange.Columns.Count;

    // Walk through first line and skip first columns for slight speed improvement
    for (int i = 7; i <= colCount; i++)
    {
        // We have found a corresponding header 
        if (xlRange.Cells[1, i].Value2.ToString().Equals(GetColName(clientname), StringComparison.CurrentCultureIgnoreCase))
        {
            WriteLog(logFile, "Found " + clientname + " at [1," + i + "]", true, true); 

            // Walking down the column 
            for (int j = 2; j < rowCount; j++)
            {
                // Find cells matching *d 
                if (xlRange.Cells[j, i] != null && xlRange.Cells[j, i].Value2 != null && (xlRange.Cells[j, i].Value2.ToString()).EndsWith("d"))
                {
                    WriteLog(logFile, "Found default printer definition at [" + j + "," + i + "] which is: " + xlRange.Cells[j, 1].Value2.ToString(), true, true); 
                    defaultPrinter = xlRange.Cells[j, 1].Value2.ToString();

                    // Jump out of first for loop
                    break; 
                }
            }

            // If no default printer has been found, exit
            if (defaultPrinter == null)
            {
                WriteLog(logFile, "No default printer found, exit!", true, true);

                // Clean up objects so excel process does not remain open
                xlWorkbook.Close();
                xlWorkbooks.Close();
                xlApplication.Quit();

                Marshal.ReleaseComObject(xlRange);
                Marshal.ReleaseComObject(xlWorkbook);
                Marshal.ReleaseComObject(xlWorkbooks);
                Marshal.ReleaseComObject(xlApplication);

                // Quit application 
                Environment.Exit(-1);
            }

            break; 
        }
    }

    // Clean up objects so excel process does not remain open
    WriteLog(logFile, "Closing excel", true, false);

    xlWorkbook.Close();
    xlWorkbooks.Close();
    xlApplication.Quit();

    WriteLog(logFile, "Releasing COM objects", true, false); 

    Marshal.ReleaseComObject(xlRange);
    Marshal.ReleaseComObject(xlWorkbook);
    Marshal.ReleaseComObject(xlWorkbooks);
    Marshal.ReleaseComObject(xlApplication);
}

0 个答案:

没有答案