Search code examples
.netexcelepplus

EPPlus returns number instead of date/string


I'm trying to read a .xlsx file using EPPlus.

I can load the worksheet and read all cells except for the cells holding dates which returns a decimal number instead of the value in the cell.

FileInfo fileInfo = new FileInfo(filePath);

using (ExcelPackage excel = new ExcelPackage(fileInfo))
{
    var worksheet = excel.Workbook.Worksheets.First();

    string cellName = worksheet.Cells[i, colDate].ToString(); // = "A2"- correct cell
    string cellValue= worksheet.Cells[i, colDate].Value.ToString(); //  = "42874.6666666667"
}

Expected returned value for Cell["A2"] of that sheet is "5/19/2017 4:00:00 PM" but I recieve "42874.6666666667".

Getting the number value in the cell next to that one works fine so I load the right file and sheet.

How can I get the date of that cell (or at least the string and parse it)?


Solution

  • Excel stores dates as the number of days since 1st January 1900 (or maybe 1904 on Mac), Using some sort of time library in your programming language, create the a date 1st January 1900 and add whatever value you have in each cell - in your example - 42874.6666666667 days. This should, in theory, give you the date in the cell.