views:

230

answers:

2

I'm using Apache POI 3.6, I want to read an excel file having date like "8/23/1991".

 switch (cell.getCellType()) {

   ...
   ...

   case HSSFCell.CELL_TYPE_NUMERIC:
     value = "NUMERIC value=" + cell.getNumericCellValue();
     break;

   ...

 }

But it takes the numeric value type and returns the value like this "33473.0". I've tried some level(with in the Numeric Cell Type),

  dbltemp=row.getCell(c, Row.CREATE_NULL_AS_BLANK).getNumericCellValue();


                        if(c == 6 || c == 9)  {
                            strTemp= new String(dbltemp.toString().trim());

                            long tempDate = Long.parseLong(strTemp);
                            Date date=new Date(tempDate);

                            strVal=date.toString();
                        }

yet I can't find it.

Thanks for those who can help....

A: 

You need the DateUtils: see this article for details.

Or, better yet, use Andy Khan's JExcel instead of POI.

duffymo
I've almost implemented many things in POI, I'm so sorry that I can't go for any just for this.. There must be something.. Yet, Thank you duffymo
venJava
DateUtils will sort you out if you can't switch to JExcel
duffymo
+3  A: 

If you know which cell i.e. column position say 0 in each row is going to be a date, you can go for row.getCell(0).getDateCellValue() directly.
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html#getDateCellValue()

For a more generic case, look at this code example for handling CELL_TYPE_NUMERIC into a date, line 151 onwards

UPDATE: Here is an example - you can apply this in your switch case code above. I am checking and printing the Numeric as well as Date value. In this case the first column in my sheet has dates, hence I use row.getCell(0).

You can use the if (HSSFDateUtil.isCellDateFormatted .. code block directly in your switch case.

if (row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
    System.out.println ("Row No.: " + row.getRowNum ()+ " " + row.getCell(0).getNumericCellValue());

                 if (HSSFDateUtil.isCellDateFormatted(row.getCell(0)))
             {

                 System.out.println ("Row No.: " + row.getRowNum ()+ " " + row.getCell(0).getDateCellValue());

                }

the output is

Row No.: 0 39281.0
Row No.: 0 Wed Jul 18 00:00:00 IST 2007
Row No.: 1 39491.0
Row No.: 1 Wed Feb 13 00:00:00 IST 2008
Row No.: 2 39311.0
Row No.: 2 Fri Aug 17 00:00:00 IST 2007
JoseK
I can't get any values... If you could specify a code snippet, that would be great...
venJava
updated to icnlude my code
JoseK