2011-07-26 21 views
0

私はExcelシートのセルにDateTime.Now.ToUniversalTime().ToLongTimeString()と入力して保存した後に閉じますが、同じExcelシートを開いてセル値を取得しようとすると、私には二重の価値が与えられます。C#を使用してExcelシートに保存された正確な値を取得する方法は?

私のC#コード:コンストラクタで

: -

Excel.Application xlApp; 
     Excel.Workbook xlWorkBook; 
     Excel.Worksheet xlWorkSheet, xlWorkSheet2, xlWorkSheet3; 
     object misValue = System.Reflection.Missing.Value; 
     Excel.Range range; 
     xlWorkBook = xlApp.Workbooks.Open(
            "Book1.xls", 
            0, 
            true, 
            misValue, 
            "", 
            "", 
            true, 
            Excel.XlPlatform.xlWindows, 
            "\t", 
            false, 
            false, 
            0, 
            true, 
            1, 
            0); 

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

((Excel.Range)xlWorkSheet.Cells[1, 5]).EntireColumn.NumberFormat = "H:mm:ss"; 
xlWorkSheet.Cells[(rowcnt + 1), 5] = DateTime.Now.ToUniversalTime().ToLongTimeString(); 

xlWorkBook.SaveAs("Book2.xls",Excel.XlFileFormat.xlOpenXMLWorkbook);     

//Close the Excel Workbook after saving a copy of it. 
xlWorkBook.Close(true, misValue, misValue); 

xlApp.Quit(); 

Marshal.ReleaseComObject(xlWorkSheet); 

Marshal.ReleaseComObject(xlWorkBook); 

Marshal.ReleaseComObject(xlApp); 

xlWorkBook = null; 

//Make the Excel Application null. 
xlApp = null; 

、その後、セルの値を取得するには、以下のメソッドを呼び出します。

private void AddXMLDetail() 
     { 
      try 
      { 
       xlApp = new Excel.ApplicationClass(); 

       #region Open the Excel File 

       //Assigning the saved changes workbook present in 
       //"My Document" to add the xml detail in excel sheet. 
       xlWorkBook = 
        xlApp.Workbooks.Open(
          "Book2.xls", 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing, 
          Type.Missing); 

       #endregion Open the Excel File. 

       //Assigning Sheet 1. 
       xlWorkSheet = 
        (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

       string compareTime = 
         ((Excel.Range)range.Cells[1, 5]).Value2.ToString(); 

       Console.WriteLine("The saved cell value is : " + compareTime); 

       #region Close and Release the object. 

       //Close the Excel Workbook after saving a copy of it. 
       xlWorkBook.Close(true, misValue, misValue); 

       Marshal.ReleaseComObject(xlWorkSheet); 
       Marshal.ReleaseComObject(xlWorkBook); 

       xlWorkBook = null; 

       //Quit The Excel Application after success completion of work. 
       xlApp.Quit();    

       //Release the Excel Application for reuse. 
       Marshal.ReleaseComObject(xlApp); 

       //Make the Excel Application null. 
       xlApp = null; 

       #endregion Close and Release the object. 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("exception was : " + ex); 
      } 
     } 

私が間違っていることを誰に教えてもらえますか?答えは高く評価されます。

答えて

1

テキストなどの細胞のnumberFormat

((Excel.Range)xlWorkSheet.Cells[1,5]).EntireColumn.NumberFormat = "@"; 

その後は、あなたが今やっているなどの文字列変数に同じ値をフェッチしようとしてください。

+0

ありがとうございました。 – Turtleneck

関連する問題