用C#关闭Excel的问题

我已经用此代码进行了单元测试:

  Excel.Application objExcel = new Excel.Application();
            Excel.Workbook objWorkbook = (Excel.Workbook)(objExcel.Workbooks._Open(@"D:\Selenium\wszystkieSeba2.xls", true,
            false, Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value, Missing.Value,
            Missing.Value));


            Excel.Worksheet ws = (Excel.Worksheet)objWorkbook.Sheets[1];
            Excel.Range r = ws.get_Range("A1", "I2575");
            DateTime dt = DateTime.Now;
            Excel.Range cellData = null;
            Excel.Range cellKwota = null;
            string cellValueData = null;
            string cellValueKwota = null;
            double dataTransakcji = 0;
            string dzien = null;
            string miesiac = null;
            int nrOperacji = 1;
            int wierszPoczatkowy = 11;
            int pozostało = 526;

            cellData = r.Cells[wierszPoczatkowy, 1] as Excel.Range;
            cellKwota = r.Cells[wierszPoczatkowy, 6] as Excel.Range;


            if ((cellData != null) && (cellKwota != null))
            {

                object valData = cellData.Value2;
                object valKwota = cellKwota.Value2;


                if ((valData != null) && (valKwota != null))
                {
                    cellValueData = valData.ToString();
                   dataTransakcji = Convert.ToDouble(cellValueData);
                   Console.WriteLine("data transakcji to: " + dataTransakcji);
                    dt = DateTime.FromOADate((double)dataTransakcji);
                   dzien = dt.Day.ToString();
                   miesiac = dt.Month.ToString();


                    cellValueKwota = valKwota.ToString();


                }
  }

                       r.Cells[wierszPoczatkowy, 8] = "ok";
            objWorkbook.Save();



            objWorkbook.Close(true, @"C:\Documents and Settings\Administrator\Pulpit\Selenium\wszystkieSeba2.xls", true);
                objExcel.Quit();

为什么在完成测试后我仍在处理过程中表现出色(它没有关闭)

并且:有什么我可以改善的性能吗?

Excel 2007和.net 3.5

解决方法:

我使用这样的代码段来强制关闭它:

    public void DisposeExcelInstance()
    {
        //oXL.DisplayAlerts = false;
        //oWB.Close(null, null, null);
        //oXL.Quit();


        //oWB.Close(null, null, null);
        //oXL.Quit();
        ///KNG - CLEANUP code
        oXL.DisplayAlerts = false;
        oWB.Close(null, null, null);
        oXL.Workbooks.Close();
        oXL.Quit();
        if (oResizeRange != null)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oResizeRange);
        if (oSheet != null) 
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
        if (oWB != null)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
        if (oXL != null)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
        oSheet = null;
        oWB = null;
        oXL = null;
        GC.Collect(); // force final cleanup!

    }
上一篇:用零初始化javascript数组


下一篇:mysql-我需要一些帮助来优化我的数据库架构