Apach POI 如何拿到有公式的单元格,计算结果

   public static void getFormulaCellValue(){
       
       FileInputStream fis = new FileInputStream("c:/temp/test.xls");
       Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("c:/temp/test.xls")
       Sheet sheet = wb.getSheetAt(0);
       FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

       
       Row row = sheet.getRow(1);
       Cell cell = row.getCell(1); 

       CellValue cellValue = evaluator.evaluate(cell);

       switch (cellValue.getCellType()) {
           case Cell.CELL_TYPE_BOOLEAN:
               System.out.println(cellValue.getBooleanValue());
               break;
           case Cell.CELL_TYPE_NUMERIC:
               System.out.println(cellValue.getNumberValue());
               break;
           case Cell.CELL_TYPE_STRING:
               System.out.println(cellValue.getStringValue());
               break;
           case Cell.CELL_TYPE_BLANK:
               break;
           case Cell.CELL_TYPE_ERROR:
               break;

       }    
   }

 

posted @ 2017-12-19 14:25  雨 燕  阅读(640)  评论(0编辑  收藏  举报