Read a date value in a cell

Mar 2, 2010 at 9:43 AM
Edited Mar 2, 2010 at 3:52 PM

Hello,

I read an Excel file. I don’t know before what kind of data are in this file.

When I read a cell with a date value, I have the number and not the date... :(

The "HSSFCell.CELL_TYPE_DATE" type doesn’t exist. How can I know if a cell value is a date or no ?

 

        private string GetCellValue(int row, int col)
        {
            String value = "";

            try {
                HSSFCell cell = this.workSheet.GetRow(row-1).GetCell(col-1);

                if (cell.CellType != HSSFCell.CELL_TYPE_BLANK)
                {
                    switch (cell.CellType)
                    {
                        case HSSFCell.CELL_TYPE_NUMERIC:
                            // ********* Date comes here ************

                            // Numeric type
                            value = cell.NumericCellValue.ToString();

                            break;

                        case HSSFCell.CELL_TYPE_BOOLEAN:
                            // Boolean type
                            value = cell.BooleanCellValue.ToString();
                            break;

                        default:
                            // String type
                            value = cell.StringCellValue;
                            break;
                    }
                }

            } catch (Exception) {
                value = "";
            }

            return value.Trim();
        }

 

Thanks,

Samic

Mar 2, 2010 at 3:52 PM
Edited Mar 2, 2010 at 3:53 PM

Finally, I have used this following statement to get the cell data format :

 

HSSFCell cell = this.workSheet.GetRow(row-1).GetCell(col-1);
String format = cell.CellStyle.GetDataFormatString();

 

Samic

Finally, I used the following statement to retrieve the format of the cell.
Apr 27, 2011 at 10:55 AM

The simplest way in to use provided DateUtil class

 

    if (DateUtil.IsCellDateFormatted(cell))
    {
        DateTime date = DateUtil.GetJavaDate(cell.NumericCellValue);
        //......
    }