java - 从现有 Excel 中提取具有 2 个精度值 (3.55) 的特定列。帮我制作一下,它有1个精度值(3.5,3.9)

标签 java excel apache-poi

我想从现有的 Excel 中提取具有 2 个精度浮点值(3.55,3.98)的特定列。请帮助我使其具有 1 个精度值(3.5,3.9)。

try {
    InputStream myxls = new FileInputStream("D:\\Amrutha\\SIMPL\\DashBoardReport20110228.xls");
    HSSFWorkbook wb = new HSSFWorkbook(myxls);
    HSSFSheet sheet = wb.getSheetAt(0);                  

    HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet,wb);
    CellReference cellReferenceSource = new CellReference("AA17");
    HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);

    for(Iterator rowIter = sheet.rowIterator(); rowIter.hasNext(); ) {
       HSSFRow  row = (HSSFRow) rowIter.next();
       HSSFCell cellSource = row.getCell(cellReferenceSource.getCol());

       evaluator.setCurrentRow(row);
       if (cellSource != null) {
           //HSSFFormulaEvaluator.CellValue cellValue = evaluator.evaluate(cell);
           CellValue cellValue= evaluator.evaluate(cellSource);
           //CellValue cellValueTarget= evaluator.evaluate(cellTarget);
           FileOutputStream fout = new FileOutputStream("D:\\Amrutha\\SIMPL\\DashBoardReport20110228.xls");

           if(cellValue != null) {

              switch (cellValue.getCellType()) {

              case HSSFCell.CELL_TYPE_BOOLEAN:
                  System.out.println(cellValue.getBooleanValue());                      
                  break;
              case HSSFCell.CELL_TYPE_NUMERIC:
                  // HSSFCellStyle style = wb.createCellStyle();
                  HSSFDataFormat format = wb.createDataFormat();
                  HSSFCellStyle style = wb.createCellStyle();
                  style.setDataFormat(format.getFormat("0.0"));
                  // cellSource.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                  cellSource.setCellStyle(style);
                  System.out.println( cellSource.getCellStyle());
                  // style.setDataFormat(format.getFormat(".0"));
                  System.out.println(cellValue.getNumberValue());
                  cellSource.setCellValue(cellValue.getNumberValue()); 
                  // cellSource.setCellStyle(style);
                  // cellSource.setCellValue(cellValue.getNumberValue()); 
                  // System.out.println(cellValue.getNumberValue()); 
                  wb.write(fout);
                  fout.close();
                  break;
              case HSSFCell.CELL_TYPE_STRING:
                  System.out.println(cellValue.getStringValue());                      
                  break;
              case HSSFCell.CELL_TYPE_ERROR:
                  System.out.println(cellValue.getErrorValue());                      
                  break;
              case HSSFCell.CELL_TYPE_BLANK:
                  System.out.println("");                      
                  break;                  
                  // CELL_TYPE_FORMULA will never happen
              case HSSFCell.CELL_TYPE_FORMULA:
                  System.out.println("CELL_TYPE_FORMULA");
                  break;
              default:
                  System.out.println("null");
                  break;
              }
           }
        } 
        else {
            System.out.println("null");
        }
     }
  } 
  catch(Exception e) {
      System.out.print("Exception in main "+e);
  }
}

最佳答案

您可以使用简单的 java 而不是 poi 来完成此操作。

Cell.getNumericCellValue 将返回一个 Double,因此只需将其格式化为您想要的精度即可。

        Double d = new Double(cellSource.getNumericCellValue());
        System.out.println ("Double value "+ d);

        DecimalFormat df = new DecimalFormat("###.#");
        System.out.println(df.format(d));

或者更简单

DecimalFormat df = new DecimalFormat("###.#");
                System.out.println(df.format(cellSource.getNumericCellValue()));

我的试验结果

Double value 1.808702175543886
1.8

关于java - 从现有 Excel 中提取具有 2 个精度值 (3.55) 的特定列。帮我制作一下,它有1个精度值(3.5,3.9),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5166113/

相关文章:

java - 使用java读取excel中嵌入的对象

java - Maven jar 插件 - SNAPSHOT 依赖项的错误类路径条目

java - 连接 Google+ 登录时应如何使用 Google Api Key?

java - Android Sqlite3 - 由命令行创建的列,但不是从我的 Android 代码创建的

java - 无效行号 (-32768) 超出允许范围 (0..1048575)

png - 使用Apache Poi将ppt转换为png

java - Spring Boot中无法建立一对一关系,@JoinColumns(列不存在)

excel - 使用 VBA 在 Excel 工作表中插入公式的问题

javascript - 无法访问创建的工作簿的工作表

python - 写一份 list 以取得优异成绩