Domanda

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!

È stato utile?

Soluzione

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
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top