0

All,

In .NET, I'm using ClosedXML to read in an uploaded Excel file and converting the cells to a 2D array of strings. The end goal is to modify certain cells and display the modified cells to the user.

I'm currently having trouble handling cells that store DateTime values. Weirdly, when I read the cell's data type, it returns XLCellValues.Number (not XLCellValues.DateTime, which is what I would expect). I've found that Excel actually stores DateTimes as doubles with some metadata saying to convert it to a date, which might explain that issue.

I'm wondering if anyone knows of a way, using ClosedXML, to check if an Excel cell's value is a DateTime type? And, if so, how I can convert that value to a .NET DateTime?

Thanks!

bclarke
  • 15
  • 1
  • 5

1 Answers1

0

Without looking at your file I'd say the most likely cause is that the cell does contain a number which is then formatted as a date. This is perfectly valid in Excel, in fact all dates are stored as numbers.

Use the following to check if a cell contains a date:

DateTime date;
if (cell.TryGetValue(out date))
{
    // Got a date
}
Manuel
  • 10,869
  • 14
  • 55
  • 86
  • Sorry for the delayed response; I've been out of town. Where is TryGetValue defined? I have a cell of type IXLCell, and this isn't a defined function for .NET C#. My cells are formatted in Excel using the Time category (formatted like 7:50 AM). I'm happy to provide any other information you need, just let me know. – bclarke Apr 30 '13 at 00:37
  • Might not have been pushed out on the last release. Get the latest source code and compile it. – Manuel Apr 30 '13 at 03:11