45

I am currently using Apache POI for Java to set formulas in cells.

But after I run the program and open the Excel file that I created and processed, the cells with the formula include the formula as a string, rather than the value the formula should have returned.

5
  • Are you using setCellFormula (poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/…)? Feb 26, 2010 at 1:41
  • yep. Basically my formula is a user defined one, not supported by defualt by excel. Its something like this: "=IQLink|yhoo!change" All it does it gets the stocks current price via its stock program. But point being is the formula is not supported by excel by default and does that have anything to do with it?
    – vamsi
    Feb 26, 2010 at 1:46
  • What happens if you tell Excel to manually recalculate all formulas (office.microsoft.com/en-us/excel/…)? Feb 26, 2010 at 4:29
  • 1
    when i go into the excel and manually recalculate, they work as expected...i don't know why poi can't do the same.
    – vamsi
    Feb 26, 2010 at 9:19
  • I dont believe poi can do any formula evaluating.
    – mP.
    Mar 17, 2011 at 2:11

8 Answers 8

60

The HSSFCell object has methods .setCellType and .setCellFormula which you need to call like this:

// "cell" object previously created or looked up
String strFormula= "SUM(A1:A10)";
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula(strFormula);
5
  • 1
    Nope that didn't work. Basically im using a custom formula that is not by default supported by excel. Would there be any problems with user defined formulas?
    – vamsi
    Feb 26, 2010 at 3:43
  • I wouldn't have thought so, but then I've never tried it. I see no reason why not; it should just fill the cell with =strFormula but perhaps it does something else.
    – user257111
    Feb 26, 2010 at 3:55
  • The method used in this post is deprecated. @Jinu P C has an answer that will work with more recent versions.
    – Tim
    May 5, 2017 at 13:01
  • how to rewrite the formula in term of row number and cell number ? I am generating the spread sheet programatically so i am not sure with the A, B, C terminology Jun 17, 2018 at 20:02
  • This answer is now deprecated
    – ATNPGO
    Nov 18, 2020 at 0:56
20

Cell Constants are deprecated and will be removed from version 4.0 instead of Cell Use

CellType.FORMULA

String formula= "SUM(B4:B20)"; cell.setCellType(CellType.FORMULA); cell.setCellFormula(formula);

UPDATE

setCellType() :Based on @fenix comment. This method is deprecated and will be removed in POI 5.0. Use explicit setCellFormula(String), setCellValue(...) or setBlank() to get the desired result.

String formula= "SUM(B4:B20)";
cell.setCellFormula(formula);
2
  • is there any way I can add a custom formula to this? i.e. "=DATEDIF(A2,NOW(),\"y\")" using the method above will throw an exception for any custom formulas.
    – Tim
    May 5, 2017 at 13:27
  • 2
    Cell.setCellType(CellType) is deprecated as well (will be removed from Apache POI 5.0). Using explicit Cell.setCellFormula(String) should be enough.
    – Fenix
    Aug 9, 2020 at 21:48
13

The below code worked fine for me, hope this could be useful to someone.

cell.setCellType(Cell.CELL_TYPE_FORMULA);
cell.setCellFormula("SUM(C70:C76)");
0
7

Apache POI does not support user-defined functions.

From the documentation:

Note that user-defined functions are not supported, and is not likely to done any time soon... at least, not till there is a VB implementation in Java!

6

You could use this to evaluate the formulas in the workbook:

        // Evaluate all formulas in the sheet, to update their value
    FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
    formulaEvaluator.evaluateAll();
4

I was having the similar problem and "SUM(B4:B20)" does not work for me directly as the sheet was generated dynamically. The problem was with the cell reference.

On the basis of https://stackoverflow.com/a/2339262/2437655 and https://stackoverflow.com/a/33098060/2437655 I was able to generate the actual formula. e.g.

 val totalProductQtyPerUserCell = userDetailsRow.createCell(products.size + 1)
 totalProductQtyPerUserCell.cellType = HSSFCell.CELL_TYPE_FORMULA
 totalProductQtyPerUserCell.cellFormula = "SUM(${CellReference.convertNumToColString(1)}${totalProductQtyPerUserCell.row.rowNum + 1}:${CellReference.convertNumToColString(products.size)}${totalProductQtyPerUserCell.row.rowNum + 1})"

Hope that help.

1

Here is one suggested way:

Workbook workbook1 = new SXSSFWorkbook(-1);
Sheet newSheet = workbook1.createSheet("Valuations");
Row newRow = newSheet.createRow(i-1);
newRow.createCell(L++).setCellFormula("AVERAGE(" + "E" + i + ":" + "G" + i + ")");

Where E and G are the columns like A1,B1,C1....E1...G1
and i represent a number in the above Column.

L++ is simply an incremental counter.

0

Instead of only setting the formula like this:

cell.setCellFormula(strFormula);

You additionally should evaluate this single cell ( - no need to "formulaEvaluator.evaluateAll()" as Diogo Vilela wrote.)

FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
formulaEvaluator.evaluateFormulaCell(cell);

This is needed, if you e.g. execute a "sheet.autoSizeColumn(...)" later on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.