1

Hi I parse excel table with Microsoft.Office.Interop.Excel. Here is my code which parse excel table to data table. It works good.

    public static DataTable ImportExcelToDataTable(String path)
    {
        var app = new Application {Visible = false};

        Workbook workBook = app.Workbooks.Open(path, 0, true, 5, "", "", 
            true, XlPlatform.xlWindows, Type.Missing, false,
        false, 0, true, 1, 0);
        Sheets sheets = workBook.Worksheets;

        var activeSheet = (Worksheet) sheets.Item[1];
        Range activeSheetRange = activeSheet.UsedRange;

        var dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");

        for (int i = 1; i <= activeSheetRange.Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            for (int j = 1; j <= activeSheetRange.Columns.Count; j++)
            {
                string value = ((Range) activeSheetRange.Cells[i, j]).Value2.ToString();
                dr[j - 1] = value;
            }
            dt.Rows.Add(dr);
            dt.AcceptChanges();
        }

        app.Quit();
        app.Workbooks.Close();
        GC.Collect();
        return dt;
    }

Problem I run app (Console) invoke this method and close console app. If I check in TaskManager process I see that Excel.exe process is still runing.

If I call this method 100-times I have 100 excel.exe processes in memmory.

It is normal ? How can solve it.

Thank for your time and cooperation

khellang
  • 17,550
  • 6
  • 64
  • 84

1 Answers1

0

You might need to use Marshal.ReleaseComObject - see the answers to this question and this question.

Community
  • 1
  • 1
E.Z. Hart
  • 5,717
  • 1
  • 30
  • 24