简体   繁体   中英

Evaluate formula and remove formula from cell

I want to evaluate formula and the remove the formula from the cell keeping its values. How can this be done using Apache POI ?

Below is my code where I evaluate all the formulas.

        FormulaEvaluator evaluator = template.getCreationHelper().createFormulaEvaluator();
        int sheetCount = template.getNumberOfSheets();
        for(int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex ++) {
            sheet = dfaTemplate.getSheetAt(sheetIndex);
            for (Row r : sheet) {
                for (Cell c : r) {
                    if (c.getCellType() == Cell.CELL_TYPE_FORMULA) {
                        evaluator.evaluateFormulaCell(c);
                    }
                }
            }
        }

You should use evaluator.evaluateInCell(cell) instead of evaluator.evaluateFormulaCell(c) . If the cell contains a formula, evaluateInCell evaluates the formula, and puts the formula result back into the cell, in place of the old formula.

See evaluator.evaluateInCell(cell) for further information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM