Search code examples
excelapacheapache-poixssfhssf

org.apache.poi writing xls formula of cells sum


I have to write xls formula that will sum few cells from one column. The output in xls of that formula look that:

SUM(F5;F9;F13;F16)

I've created that formula in org.apache.poi library.

Cell cell = rowSum.createCell(j);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);

// column number like (A or B) in excel to use in formula
String x = CellReference.convertNumToColString(j);
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);

// for total sum row
String totalSumFormula = "SUM(";

for(int s=0; s<sumRowNumbers.size(); s++) {
int tempSumRowNumber = sumRowNumbers.get(s);
tempSumRowNumber++;
    totalSumFormula += (x + tempSumRowNumber);

    if(s+1 != sumRowNumbers.size()) {
        totalSumFormula += ";";
} else {
    totalSumFormula += ")";
    }
 }
 cell.setCellFormula(totalSumFormula);

but unfortunatelly I am getting an error that I cant understand:

[FormulaParseException: Parse error near char 6 ';' in specified formula 'SUM(F5;F9;F13;F16)'. Expected ',' or ')'] 

Can You give me any advise? How to deal with it?


Solution

  • Hey i found solution for my problem. I've should use seperator: "," instead of ";".

    Now it works like a charm. Proper generated formula in library:

    SUM(F5,F9,F13,F16)
    

    which generates formula in excel file:

    =SUM(F5;F9;F13;F16)