Search code examples
c#excelcominterop

Getting Excel to shutdown after interop


I have an app that sucks some data out of an Excel spreadsheet. I can't get it to clean up nicely. EXCEL.EXE continues to run. I've read other posts that say you need to Marshal.ReleaseComObject() on all the COM objects. I believe I am doing that correctly.

To make things a bit cleaner, I've removed error checking code (none of it Excel related), but this is the code:

public override MarketPriceItem[] GetMarketPrices()
{
    string[] files = Directory.GetFiles(ConfigurationManager.AppSettings["XXSpreadsheets"]);
    Excel.Application app = new Excel.Application { Visible = false };
    List<MarketPriceItem> prices = new List<MarketPriceItem>();
    Excel.Workbooks workbooks = app.Workbooks;
    foreach (string filename in files)
    {
        Excel.Workbook wb = null;
        wb = workbooks.Open(filename);

        Excel.Worksheet sheet = wb.Sheets[1];

        cell = sheet.Cells[1, 4];
        string dateStr = cell.Text;
        Marshal.ReleaseComObject(cell);

        DateTime friday = DateTime.Parse(dateStr);
        DateTime today = DateTime.Today;

        int days = 5 - (friday - today).Days;
        int todayCell = (days * 2) + 1;
        int rows = sheet.UsedRange.Rows.Count;

        foreach (int key in _codeToDescription.Keys)
        {
            for (int index = 4; index < rows; index++)
            {
                cell = sheet.Cells[index, 1];
                string desc = cell.Text;
                Marshal.ReleaseComObject(cell);
                if (desc.Trim() == _codeToDescription[key].Trim())
                {
                    cell = sheet.Cells[index, todayCell];
                    if (cell == null)
                    {
                        Marshal.ReleaseComObject(cell);
                        continue;
                    }
                    var valVal = cell.Value;
                    Marshal.ReleaseComObject(cell);
                    if (valVal == null)
                    {
                        continue;
                    }
                    prices.Add(new MarketPriceItem()
                    {
                        MarketCode = key.ToString(),
                        MarketDate = today,
                        MarketTypeCode = "XX",
                        Price = (decimal) valVal
                    });
                }
            }
        }
        Marshal.ReleaseComObject(sheet);
        Marshal.ReleaseComObject(wb);
    }
    Marshal.ReleaseComObject(workbooks);
    Marshal.ReleaseComObject(app);
    return prices.ToArray();
}

What am I missing?


Solution

  • At the end of the code, have you tried

    app.Quit();
    

    That should quit the instance of Excel that is running.