Problem with closing excel by c#

Posted by phenevo on Stack Overflow See other posts from Stack Overflow or by phenevo
Published on 2010-03-24T07:21:12Z Indexed on 2010/03/24 7:23 UTC
Read the original article Hit count: 449

Filed under:
|
|

Hi, I've got unit test with this code:

  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 pozostalo = 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();

Why after finish test I'm still having excel in process (it does'nt close)

And : is there something I can improve to better perfomance ??

© Stack Overflow or respective owner

Related posts about c#

Related posts about excel