如何使用C#获取Excel工作表中保存的确切值?

时间:2011-07-26 07:18:06

标签: c# excel visual-studio vbscript

我在excel表格的单元格中输入DateTime.Now.ToUniversalTime().ToLongTimeString()并在保存后关闭它,但在再次打开相同的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 个答案:

答案 0 :(得分:1)

将单元格numberFormat设为文本

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

然后尝试像现在一样获取字符串变量的相同值。