How to set formulas in cells using Apache POI?

后端 未结 7 1638
慢半拍i
慢半拍i 2021-01-01 09:09

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 t

相关标签:
7条回答
  • 2021-01-01 09:35

    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.

    0 讨论(0)
提交回复
热议问题