Question

Je suis en train d'utiliser POI XSSF pour évaluer certaines formules Excel. Les valeurs ne doivent pas être sauvés, et je peux avoir à calculer de nombreuses formules, donc je suis en train de tout faire dans la même cellule.

Le problème est que la valeur de la cellule semble se coincer sur la première formule est entrée même après recalcul

FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
XSSFCell formulaCell = row.createCell(6);
formulaCell.setCellFormula("Date(2011,10,6)");
CellValue cellValue = evaluator.evaluate(formulaCell);
System.out.println(cellValue.getNumberValue());

formulaCell.setCellFormula("Date(1911,3,4)");
cellValue = evaluator.evaluate(formulaCell);
System.out.println(cellValue.getNumberValue());

Sorties 40822,0 40822,0 (Excel équivalent d'06/10/2011) à chaque fois au lieu de réévaluer la nouvelle formule.

Était-ce utile?

La solution

If you use the formulaEvaluator more than once, you need this line in between uses, or else it uses the same result each time.

formulaEvaluator.clearAllCachedResultValues()

Autres conseils

The FormulaEvaluator caches cell calculated values to speed up processing. If you perform cell updates after creating the evaluator, then you need to tell it!

See the FormulaEvaluator documentation for more details. For you case, try:

formulaCell.setCellFormula("Date(1911,3,4)");
evaluator.notifySetFormula(formulaCell);
cellValue = evaluator.evaluate(formulaCell);
System.out.println(cellValue.getNumberValue());

You can use following steps to get your work done. These are two solutions out of which you can make use of any one function. It evaluates the complete workbook so whatever formula you use would get evaluated. Hope this helps.

1) evaluator.evaluateAll(); 2) XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top