user3214269
user3214269

Reputation: 219

Reading xl sheeet by using java code

Here I am trying to read xl sheet by using following java code but i am getting the exception while reading cell as below.

Xl reading code

Cell cell14 = row.getCell(14);
int celltype = cell4.getCellType();
if (celltype == HSSFCell.CELL_TYPE_BOOLEAN) {
    System.out.println("inide boolean");
    row.getCell(14).getBooleanCellValue();
    System.out.println("row.getCell(14).getBooleanCellValue()" + row.getCell(14).getBooleanCellValue());
} else if (celltype == HSSFCell.CELL_TYPE_ERROR) {
    System.out.println("inide CELL_TYPE_ERROR");
    row.getCell(14).getErrorCellValue();
    System.out.println("row.getCell(14).getErrorCellValue()" + row.getCell(14).getErrorCellValue());
} else if (celltype == HSSFCell.CELL_TYPE_FORMULA) {
    System.out.println("inide CELL_TYPE_FORMULA");
    row.getCell(14).getCellFormula();
    System.out.println(" row.getCell(14).getCellFormula()" + row.getCell(14).getCellFormula());
} else if (celltype == HSSFCell.CELL_TYPE_NUMERIC) {
    System.out.println("inide CELL_TYPE_NUMERIC");
    row.getCell(14).getNumericCellValue();
    System.out.println("row.getCell(14).getNumericCellValue()" + row.getCell(14).getNumericCellValue());
} else if (celltype == HSSFCell.CELL_TYPE_STRING) {
    System.out.println("inide CELL_TYPE_STRING");
    row.getCell(14).getRichStringCellValue();
    System.out.println("row.getCell(14).getStringCellValue();" + row.getCell(14).getRichStringCellValue());
}

Here is the exception

java.lang.IllegalStateException: Cannot get a text value from a numeric cell
    at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:643)
    at org.apache.poi.hssf.usermodel.HSSFCell.getRichStringCellValue(HSSFCell.java:720)
    at org.apache.poi.hssf.usermodel.HSSFCell.getRichStringCellValue(HSSFCell.java:67)

Upvotes: 1

Views: 219

Answers (1)

Gábor Bakos
Gábor Bakos

Reputation: 9100

I think the following typo might be the cause of the problem:

Cell cell14 = row.getCell(14);
int celltype = cell4.getCellType();

Probably it should be:

Cell cell14 = row.getCell(14);
int celltype = cell14.getCellType();

(cellType is now based on cell14 instead of cell4.)

Upvotes: 2

Related Questions