POI Appending .0 while reading numeric data from excel

前端 未结 2 913
青春惊慌失措
青春惊慌失措 2021-01-12 20:07

I am using POI HSSF to read excel data and I am using JUnit to check the data against database proc RefCursor.

The Junit test fails as the numeric data from the Refc

2条回答
  •  一生所求
    2021-01-12 20:34

    This is virtually identical to a number of other questions here, such as returning decimal instead of string (POI jar)

    The answer is the same as the one I gave here:

    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) );
        }
     }
    

提交回复
热议问题