This project has moved and is read-only. For the latest updates, please go here.

Get DataType from Cell

Nov 4, 2011 at 11:26 AM

Hi,

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 4: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");
            Console.WriteLine(wb.Worksheet(1).Cell(1,1).DataType.ToString());

It returns "Number".

Nov 4, 2011 at 7: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 10: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