Tuesday, April 6, 2010

Problem Reading Excel Dates. Excel date gets converted to Double value in DotNet

Dates are stored in Excel as doubles, only displayed as dates per the formatting on the sheet. The Value2 property on the cells returns the stored long value. You need to convert this value to the date yourself. You can use the DateTime.FromOADate() method to do this conversion.

Example Code:


double dateTimeInExcelFormat = Convert.ToDouble(((Range)workSheet.Cells[5, 3]).Value2);
DateTime date = DateTime.FromOADate(dateTimeInExcelFormat);

No comments:

Post a Comment