Apurv
Apurv

Reputation: 3753

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);
                    }
                }
            }
        }

Upvotes: 1

Views: 1136

Answers (1)

Priyesh
Priyesh

Reputation: 2071

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.

Upvotes: 2

Related Questions