Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve percentage type value from excel sheet using poi

I am trying to read cell values excel sheet using Apache POI. One of my sheet contains percentage type as the cell type. When I read that cell using POI using cell.getNumbericValue(), its returning me the double value. If cell contains 11.24%, it is returning me 0.1124. My problem is, I want to read the % symbol also from the cell i.e., I want to read the exact data as it is in cell as 11.24%. So, I tried it using the cell.toString(), even then it is returning me the same value as above. Can anyone please suggest me, how to get rid of this problem. Thanks in advance Nandu

like image 857
Nandu Avatar asked Feb 24 '12 16:02

Nandu


1 Answers

You can detect if a cell is formatted as a percentage by testing the cell data format string like this:

if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
    if (cell.getCellStyle().getDataFormatString().contains("%")) {
        // Detect Percent Values 
        Double value = cell.getNumericCellValue() * 100;
        System.out.println("Percent value found = " + value.toString() +"%");
    } else {
        Double value = cell.getNumericCellValue();
        System.out.println("Non percent value found = " + value.toString());
    }
}

This should enable you to distinguish between percentage formatted values and ordinary numeric values.

like image 131
Wee Shetland Avatar answered Sep 16 '22 16:09

Wee Shetland