Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
4970
How to get datetime data from importing excel file?
posted

Try to import an excel file into xamgrid, but can't get right data for datatime.

in excel cell, the data like: 2012-10-10.

Code to import excel file: use OpenFileDialog to open excel file, then get data in Workbook. but data for above cell is: value =41223.0 , type = object{double}. The code is something like:

TestDate = DateTime.Parse(row.Cells[3].Value.ToString())

and this code stop the app.

How to resolve this problem?

Parents
No Data
Reply
  • 44743
    Verified Answer
    posted

    Dates are actually stored as numbers in an Excel file. Excel only displays them as dates to the user because the format string on the cell is set so that the number converts to a date. If you are using the 12.1 version of the Excel library, you can now use the GetText(...) method on cells to get the date string. Otherwise, you will need to look at the FormatString property on the cell format returned from a cell's GetResolvedCellFormat() method. If it is a date format string, you can use the ExcelCalcValue.ExcelDateToDateTime(...) static methods to convert the number to a DateTime.

Children