C# Excel进程不会以Excel简介终止

C# Excel进程不会以Excel简介终止,c#,excel-interop,C#,Excel Interop,我一直在尝试将数据从excel工作表导入数据表。问题是,在我完成后,excel不会终止该过程。我不想使用System.Diagnostics按进程名终止,因为这种方法将终止所有excel实例,而不是应用程序创建的实例。我知道这个问题曾经多次贴在这里,但似乎没有一个解决方案适合我。我可能遗漏了什么,看不见 下面是我的代码: private void importToolStripMenuItem_Click(object sender, EventArgs e) { openFil

我一直在尝试将数据从excel工作表导入
数据表
。问题是,在我完成后,excel不会终止该过程。我不想使用
System.Diagnostics
按进程名终止,因为这种方法将终止所有excel实例,而不是应用程序创建的实例。我知道这个问题曾经多次贴在这里,但似乎没有一个解决方案适合我。我可能遗漏了什么,看不见

下面是我的代码:

    private void importToolStripMenuItem_Click(object sender, EventArgs e)
{
    openFileDialog1.Filter = "Excel WorkBooks (*.xlsx)|*.xlsx";
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.Multiselect = false;


    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
            string empRange = "D4";
            string emptxid = "K4";

            object oMissing = System.Reflection.Missing.Value;
            string path = openFileDialog1.FileName;


            oExcel.Application xlApp;
            oExcel.Workbooks xlWorkBooks;
            oExcel.Workbook xlWorkBook;
            oExcel.Sheets xlSheets;
            oExcel.Worksheet xlWorkSheetDATA;
            oExcel.Worksheet xlWorkSheetEMP;
            oExcel.Range range;

            xlApp = new oExcel.Application();

            xlWorkBooks = xlApp.Workbooks;
            xlWorkBook = xlWorkBooks.Open(path);
            xlSheets = xlWorkBook.Worksheets;


            xlWorkSheetDATA = (oExcel.Worksheet)xlSheets.get_Item(DATASheetName);
            xlWorkSheetEMP = (oExcel.Worksheet)xlSheets.get_Item(EMPSheetName); 
            xlWorkSheetEMP.Activate(); 
            range = xlWorkSheetEMP.get_Range(empRange, empRange); 

            xlWorkSheetDATA.Activate();
            range = xlWorkSheetDATA.get_Range(emptxid, emptxid);

            xlApp.DisplayAlerts = false;


            xlWorkSheetDATA.Columns.ClearFormats();
            xlWorkSheetDATA.Rows.ClearFormats();

            int iTotalColumns = xlWorkSheetDATA.UsedRange.Columns.Count;
            int iTotalRows = xlWorkSheetDATA.UsedRange.Rows.Count;
            xlApp.Visible = true;

            DataTable dt = new DataTable();
            addColumns(iTotalColumns, dt);
            insertIntoDataTable(iTotalRows, dt, path);

            //clean-up
            System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
            range = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheetEMP);
            xlWorkSheetEMP = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheetDATA);
            xlWorkSheetDATA = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);
            xlSheets = null;
            xlWorkBook.Close(false);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            xlWorkBook = null;
            xlWorkBooks.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBooks);
            xlWorkBooks = null;
            xlApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
            xlApp = null;

        }  
    }

编写一个方法来释放对象,如本例所示

    private void ReleaseOfficeObject(object o)
    {
        Marshal.ReleaseComObject(o);
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Marshal.FinalReleaseComObject(o);
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }
那就叫它

        ReleaseOfficeObject(range);
        ReleaseOfficeObject(xlWorkSheetDATA);
        ReleaseOfficeObject(xlSheets);
        xlWorkBook.Close(false);
        ReleaseOfficeObject(xlWorkBook);
        xlWorkBook = null;
        xlWorkBooks.Close();
        ReleaseOfficeObject(xlWorkBooks);
        xlWorkBooks = null;
        xlApp.Quit();
        ReleaseOfficeObject(xlApp);

在此上下文中,您永远不需要调用
Marshal.ReleaseComObject
。运行时完全能够跟踪COM对象,并在不再引用它们时释放它们。调用
Marshal.ReleaseComObject
是一种令人困惑的反模式,遗憾的是,甚至一些Microsoft文档也错误地提出了这一点。您也不必将局部变量设置为null-当方法完成时,局部变量中的引用将超出范围

在调试版本中,您必须小心使用此类代码。方法中的引用被人为地保持活动状态,直到方法结束,以便它们仍然可以在调试器中访问。这意味着您的本地
xlApp
变量可能无法通过调用该方法内的GC来清除

您应该运行垃圾收集两次,第一次收集可能会中断引用周期,但您需要第二次收集以确保对这些引用完成所有清理

要避免此问题,您可以遵循以下模式:

private void importToolStripMenuItem_Click(object sender, EventArgs e)
{
    openFileDialog1.Filter = "Excel WorkBooks (*.xlsx)|*.xlsx";
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.Multiselect = false;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string path = openFileDialog1.FileName;

        // Call another method that wraps all the Excel COM stuff
        // That prevents debug builds from confusing the lifetime of COM references
        DoExcelStuff(path);

        // When back here, all the Excel references should be out of scope
        // Run the GC (twice) to clean up all COM references
        // (This can be pulled out into a helper method somewhere)
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

private void DoExcelStuff(string path)
{
    // All your Excel COM interop goes here

    string empRange = "D4";
    string emptxid = "K4";

    object oMissing = System.Reflection.Missing.Value;

    // ... variable declarations
    xlApp = new oExcel.Application();

    // More xlApp, workbooks etc...

    // Done - tell Excel to close
    xlApp.Quit();

    // No need for Marshal.ReleaseComObject(...) 
    // or setting variables to null here! 
}

我从来没有找到一个方法去做。。。当您调用Interop时,您所做的是通过Excel进程调用这些服务。一般来说,GC会释放资源,但Excel不存在于垃圾收集的内存中。