简体   繁体   中英

getting incorrect value from excel using apache poi

in excel the value of column is 10101101010000000000 but when im reading it in java using POI the value is changed to 10101101009999999000, can anyone give me an idea on whats going on and how can i get the exact values from the excel. i've tried setting the celltype as string and use cell.getStringCellValue() and also this new BigDecimal(cell.getNumericCellValue()).toPlainString() but im still not getting the same value as with the excel

here's my code

List<BankClassVo> data = new ArrayList<BankClassVo>();
    FileInputStream fis = new FileInputStream(new File(Constant.VALIDATION_REFERENCE_FILE_PATH + Constant.BANK_CLASSIFICATION_REF_FILE + ".xlsx"));
    XSSFWorkbook myWorkBook = new XSSFWorkbook(fis);
    XSSFSheet mySheet = myWorkBook.getSheetAt(1);
    Iterator<Row> rowIterator = mySheet.iterator();


    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Iterator<Cell> cellIterator = row.cellIterator();

        BankClassVo vo = new BankClassVo ();


            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if (cell.getColumnIndex() == 0) {
                    vo.setsClass(new BigDecimal(cell.getNumericCellValue()).toPlainString());
                }
                else if (cell.getColumnIndex() == 1) {
                    vo.setClassification(cell.getStringCellValue());
                }
            }

        data.add(vo);
    }

    myWorkBook.close();
    return data;

Use MathContext and RoundingMode . Ref

BigDecimal value = new BigDecimal(cell.getNumericCellValue(), new MathContext(10 , RoundingMode.CEILING));
System.out.println(value.toPlainString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM