Get DataType from Cell

Nov 4, 2011 at 10:26 AM


is it possible to get the current DataType from a Cell?

worksheet.Cell(1, 1).DataType.ToString() tells me everytime the Type is Text no matter if I set it in Excel to Date or something else!

Nov 4, 2011 at 3:39 PM

You're probably not putting date or numeric values in Excel, you're using text values and then formatting them. Do the following, create a new workbook in Excel, put a 1 in cell A1, save it and then open it with ClosedXML:

            var wb = new XLWorkbook(@"TheFile.xlsx");

It returns "Number".

Nov 4, 2011 at 6:30 PM

Yes but why I have to put a number in a cell to get the right DataType? If I set the DataType in Excel to a cell without value then I would expect that ClosedXml tells me that DataFormat and not just Text.

Or am I Wrong?

Nov 4, 2011 at 9:31 PM

You're confusing data type with cell format. Data type is what's inside the cell, data format is how you want Excel to display it. By default an empty cell with contain a blank text. You can format the blank text inside the cell however you want but the contents of the cell will still be a text.

cell.DataType = What kind of contents the cell has

cell.Style.NumberFormat = How are the contents formatted