How to get the formatted value of a number for a cell in Apache POI?

前端 未结 2 1361
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 02:24

I wanted to get the value of a Numeric cell as a simple string.

Suppose there the type of cell is numeric with value 90%. Now I cannot use cell.ge

相关标签:
2条回答
  • 2021-01-14 02:28

    Try

    cell.getRichStringCellValue ().getString();    
    

    Have a look at this example
    Here is Doc

    0 讨论(0)
  • 2021-01-14 02:50

    You can force the value to be returned as a String using the methods below

    HSSFDataFormatter hdf = new HSSFDataFormatter();
    System.out.println (hdf.formatCellValue(mycell));
    

    will return "90%"

    The API for this method is at http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html#formatCellValue%28org.apache.poi.ss.usermodel.Cell%29

    This works directly even with an HSSFCell

    it worked for me even when my Cell is an HSSFCell

    i've also tried this cast - which works.

    HSSFCell cell1 = (HSSFCell) row1.getCell(2);
    
    HSSFDataFormatter hdf = new HSSFDataFormatter();
    System.out.println ("formatted "+ hdf.formatCellValue(cell1));
    
    0 讨论(0)
提交回复
热议问题