returning decimal instead of string (POI jar)

后端 未结 3 1533
醉酒成梦
醉酒成梦 2020-12-07 05:46

I need to read xls or xlsx sheet. successfully I read the sheet, but it returning decimal value instead of string (eg: for 3 -- it is returning 3.0). I need to read the cell

3条回答
  •  眼角桃花
    2020-12-07 05:52

    POI is giving you the exact value that Excel has stored in the File. Generally, if you write a number in an Excel cell, Excel will store that as a number with formatting. POI provides support to do that formatting for you if you want it (most people don't - they want the numbers as numbers so they can use them)

    The class you're looking for is DataFormatter. Your code would be something like

     DataFormatter fmt = new DataFormatter();
     for (Row r : sheet) {
        for (Cell c : r) {
           CellReference cr = new CellRefence(c);
           System.out.println("Cell " + cr.formatAsString() + " is " + 
                              fmt.formatCellValue(c) );
        }
     }
    

提交回复
热议问题