首页 > 解决方案 > 如何在不使用 apache poi 的情况下从工作簿单元格中获取公式值

问题描述

我正在使用 apache poi 在工作簿中应用 IRR 公式,并且能够在 excel 表中查看值。但我得到的价值是#NUM!当我尝试使用 FormulaEvaluator 检索值时。例子

    Cell cell = contentRow.createCell(3);
    cell.setCellFormula(irrFormula + "*12");        
    FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator();
    CellValue cellValue = evaluator.evaluate(cell);
    if (Constants.NUM_ERROR.equals(cellValue.formatAsString().trim())) {
        calculateTotal.setIrrTotal(Constants.IS_NAN);
    }else {
        double irrValue = cellValue.getNumberValue();
    }

我也尝试了以下方法来获取值,但它没有检索到值。请提供您的建议

for(Cell cell : row) {
     if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        System.out.println("Formula is " + cell.getCellFormula());
        switch(cell.getCachedFormulaResultType()) {
            case Cell.CELL_TYPE_NUMERIC:
                System.out.println("Last evaluated as: " + cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                System.out.println("Last evaluated as \"" + cell.getRichStringCellValue() + "\"");
                break;
        }
     }
 } 

cell.getRawValue();

标签: javaexcelapache-poiirr

解决方案


推荐阅读