Dwane13
Dwane13

Reputation: 418

Apache poi getCell() returns wrong value

So im trying to recive cell value like '150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbols150symbol'

In some cases ill get correct (150...) value and in some reciving '78.0'. At first i thouth that i got wrong cell type in my .xls but after some work i found that they are the same. Also calling method getCellType returns me '1' and that is CELL_TYPE_STRING.

In the end its working something like this:

String value1 = getCellValue(row.getCell(0)); --150... correct value
String value2 = getCellValue(row.getCell(1)); --150... correct value
String value3 = getCellValue(row.getCell(2)); --78.0   incorrect value
 
private String getCellValue(Cell cell) {
    switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING: //similar for other cell types
            //getting cell value based on its type
    }
}  

looking for some advices and tips cause im running out of ideas, maby im missing something?

thats how my excel looks : enter image description here

p.s. there are many '150...' vals just for testing

Upvotes: 0

Views: 1414

Answers (1)

kaweesha
kaweesha

Reputation: 803

You are using getCell(int var1) to get the content in a cell. But it returns the object of Cell class. You should use methods in Cell class to get values in Excel cells. There are methods like

  • getDateCellValue()
  • getNumericCellValue()
  • getStringCellValue()

to get the cell content depending on the CellType. Using CellType, you can decide how to get the cell content.

UPDATE:

If the problem still exists, check the data type of the cell from;enter image description here

And do the necessary changes in the code to get the correct value.

Small tip: Also you can add a single quote ' at the beginning of the cell content. Then Apache POI will get the cell content as a String. The ' will not become a part of the cell content.

Upvotes: 1

Related Questions