Reputation: 216
I am able to generate the excel file but the cell which contains the formula(suppose to show the total sum of the integer values of the column) is showing value 0 initially. Then, when i edit some of the column cells it gets updated with the sum of the values of ONLY the edited cells and not adding up those of the unedited cells.
My piece of code is:
Cell cell = row.createCell(size - 2);
cell.setCellValue("Total");
cell = row.createCell(size - 1);
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula("SUM(D3:D4)");
Also, i tried by adding further these codes below the above code.
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell);
But it still gave the same result. Can anyone please tell me what is going wrong? Thanks in advance.
Upvotes: 1
Views: 6795
Reputation: 11
Use wb.setForceFormulaRecalculation(true); before closing file
it will calculate the formula
Upvotes: 1
Reputation: 2239
I do not know why it doesn't work for you, try this code, it works for me.
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(3);// Row 4
Cell cell = row.createCell(3); // Cell 'D'
cell.setCellValue(10);
row=sheet.createRow(2);// Row 3
cell=row.createCell(3);// Cell 'D'
cell.setCellValue(2);
cell=row.createCell(4);// Cell 'E'
cell.setCellValue("Total");
cell = row.createCell(5);// Cell 'F'
cell.setCellType(Cell.CELL_TYPE_FORMULA);
cell.setCellFormula("SUM(D3:D4)");
The only difference is that I use Cell.CELL_TYPE_FORMULA
instead of HSSFCell.CELL_TYPE_FORMULA
, maybe that is the problem.
Upvotes: 1