使用 apache poi 从 excel 获取不正确的值
getting incorrect value from excel using apache poi
在 excel 中列的值为 10101101010000000000 但是当我在 java 中使用 POI 读取它时,值更改为 10101101009999999000,任何人都可以告诉我发生了什么事以及如何我从 excel 中得到了准确的值。
我试过将单元格类型设置为字符串并使用 cell.getStringCellValue()
和这个 new BigDecimal(cell.getNumericCellValue()).toPlainString()
但我仍然没有得到与 excel
相同的值
这是我的代码
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;
使用 MathContext
和 RoundingMode
。 Ref
BigDecimal value = new BigDecimal(cell.getNumericCellValue(), new MathContext(10 , RoundingMode.CEILING));
System.out.println(value.toPlainString());
在 excel 中列的值为 10101101010000000000 但是当我在 java 中使用 POI 读取它时,值更改为 10101101009999999000,任何人都可以告诉我发生了什么事以及如何我从 excel 中得到了准确的值。
我试过将单元格类型设置为字符串并使用 cell.getStringCellValue()
和这个 new BigDecimal(cell.getNumericCellValue()).toPlainString()
但我仍然没有得到与 excel
这是我的代码
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;
使用 MathContext
和 RoundingMode
。 Ref
BigDecimal value = new BigDecimal(cell.getNumericCellValue(), new MathContext(10 , RoundingMode.CEILING));
System.out.println(value.toPlainString());