从现有excel中提取具有2个精度值(3.55)的特定列。帮帮我,它有1个精度值(3.5,3.9)

时间:2011-03-02 10:27:39

标签: 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);
  }
}

1 个答案:

答案 0 :(得分:2)

你可以用简单的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