0

在 Excel 中,我有这些十进制值:

TS 学士学位
5.60 4.10
10.00 10.00
10.00 10.00
10.00 10.00

解析时,它显示不同的值:

TS 学士学位
0.1375 0.85
0.0125 0.7125
0.0125 0.8125000000000001
0.0125 0.7875000000000001

我需要打印确切的值。

这是代码:

Iterator<Row> itr = sheet.iterator();
while (itr.hasNext())
{
    Row row = itr.next();
    Iterator<Cell> cellIterator = row.cellIterator();
    while (cellIterator.hasNext())
    {
      Cell cell = cellIterator.next();
      switch (cell.getCellType())
      {
        case STRING:
        System.out.print(cell.getStringCellValue() + "\t\t\t");
        break;
        case NUMERIC:    
        if (DateUtil.isCellDateFormatted(cell)) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        System.out.print(dateFormat.format(cell.getDateCellValue()) + "\t\t\t");
        } else {
           System.out.print(cell.getNumericCellValue() + "\t\t\t\t");
          }
           break;
            default:
             }
           }
            System.out.println("");
       }

我正在使用 Apache POI-4.1.1。

请有人帮我解决这个问题吗?

4

1 回答 1

0

g00se 是对的。我的问题解决了。我必须解析公式中的值。

Iterator<Row> itr = sheet.iterator();
while (itr.hasNext()) {
 Row row = itr.next();
 //iterating over each column
 Iterator<Cell> cellIterator = row.cellIterator();
 while (cellIterator.hasNext()) {
 Cell cell = cellIterator.next();
 CellType cellType = cell.getCellType();
 if (cell.getCellType() == CellType.FORMULA) {
 switch (cell.getCachedFormulaResultType()) {
 case NUMERIC:
 System.out.print(cell.getNumericCellValue() + "\t\t");
 break;
 case STRING:
 System.out.print(cell.getRichStringCellValue() + "\t\t");
 break;
    }
 }
 switch (cell.getCellType()) {
 case STRING:
//field that represents string cell type
System.out.print(cell.getStringCellValue() + "\t\t");
break;
case NUMERIC:
//field that represents number cell type
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");                                System.out.print(dateFormat.format(cell.getDateCellValue()) + "\t\t");
   } 
  else {
double roundOff = Math.round(cell.getNumericCellValue() * 100.0) / 100.0;
System.out.print(roundOff + "\t\t");
        }
         break;
         case BLANK:
         break;
         default:
       }
     }
  System.out.println("");
 }
}
于 2021-08-13T07:15:36.573 回答