简体   繁体   中英

How to get the exact value saved in Excel sheet using C#?

I am entering DateTime.Now.ToUniversalTime().ToLongTimeString() in a cell of an excel sheet and closing it after saving, but while opening again the same excel sheet and trying to fetch the cell value its giving me some double value.

My C# Code:

In Constructor:-

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;

and then calling the below method to get the cell value.

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);
            }
        }

Can anybody tell me where I am doing wrong ?Any answer will be appreciated.

Make the cells numberFormat as Text

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

Then try to fetch the same value to a string variable as you are doing now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM