Reading Date Values and Boolan Values from Excel Sheet.

Hi,

I'm very new to POI API. I can not able to print the Date Values and Boolan Values when I read the excel sheet. I have written the following code:-

cell = row.getCell((short)colIndex);

if (cell !=null){

System.out.println("Cell Type >>> " + cell.getCellType());

switch (cell.getCellType()){

case HSSFCell.CELL_TYPE_NUMERIC :

System.out.println("Numeric : " + cell.getNumericCellValue());

break;

case HSSFCell.CELL_TYPE_STRING :

System.out.println("String : " + cell.getStringCellValue());

break;

case HSSFCell.CELL_TYPE_BLANK :

System.out.println("Blank: " + cell.getStringCellValue());

break;

case HSSFCell.CELL_TYPE_BOOLEAN :

System.out.println("Boolean : " + cell.getBooleanCellValue());

break;

case HSSFCell.CELL_TYPE_ERROR :

System.out.println("Error: " + cell.getErrorCellValue());

break;

case HSSFCell.CELL_TYPE_FORMULA :

System.out.println("Formula : " + cell.getCellFormula());

break;

default :

//Date

if(HSSFDateUtil.isCellDateFormatted(cell)){

System.out.println("Date: " + cell.getDateCellValue());

}

}

}

cell.getCellType() returns NUMERIC or FORMULA or STRING, how can I check for DATE, BOOLEAN and BLANK SPACE?

Any one can help me to solve this issue?

Thanks..

[2378 byte] By [hnpattanaika] at [2007-10-3 2:36:30]
# 1

The XLS files internal's format is stored as numbers! (CELL_TYPE_NUMERIC).

So you must check that the Excel cell format is set as 'Date Format'. And then you print in Java, cell.getCellStyle().getDataFormat() value, you will note that if you use different date formats cell.getCellStyle().getDataFormat() returns many different values, for that reason you don't trust in HSSFDateUtil.isCellDateFormatted(cell) function.

This code maybe wrong (for the format numbers), but this is the idea.

private boolean isDateFormatted(HSSFCell cell) {

switch (cell.getCellStyle().getDataFormat()) {

case 14:

case 165:

case 167:

case 168:

case 169:

case 170:

case 171:

case 172:

case 173:

case 174: return true;

default: return HSSFDateUtil.isCellDateFormatted(cell);

}

}

if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)

if (isDateFormatted(cell))

Date dateValue = cell.getDateCellValue();

PS: I haven't formatted booleans but I imagine that the solution is similar

PS2: I don't speak english I hope you understand me.

mauro@offermann.cla at 2007-7-14 19:34:49 > top of Java-index,Java Essentials,Java Programming...