Formula Excel - Apache POI

2

I created an excel file using the Apache Poi API, in it I set one of the cells with a formula, but when I open the file I need to "enter" in the cell with the formula so that it will print the result in the file. Would there be a way to make this result appear automatically?

code:

destino.getSheet("Status dos Testes").getRow(2).createCell(7).setCellType(CellType.FORMULA);
destino.getSheet("Status dos Testes").getRow(2).getCell(7).setCellFormula("CONT.SE($D$6:$D$10000, Legenda!A6)");

result:

    
asked by anonymous 10.11.2017 / 17:19

1 answer

1

Look at this example:

public static void triggerFormula(HSSFWorkbook workbook){      

            FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
            HSSFSheet sheet = workbook.getSheetAt(0);
            int lastRowNo=sheet.getLastRowNum();        

            for(int rownum=0;rownum<=lastRowNo;rownum++){
            Row row;
             if (sheet.getRow(rownum)!=null){
                     row= sheet.getRow(rownum);

                  int lastCellNo=row.getLastCellNum();

                      for(int cellnum=0;cellnum<lastCellNo;cellnum++){  
                              Cell cell;
                              if(row.getCell(cellnum)!=null){
                                 cell = row.getCell(cellnum);   
                                if(Cell.CELL_TYPE_FORMULA==cell.getCellType()){
                                evaluator.evaluateFormulaCell(cell);
                            }
                        }
                     }
             }
            }


        }
    
10.11.2017 / 17:53