Formula Excel - Apache POI

Asked

Viewed 561 times

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" the cell with the formula so that it prints the result in the file. There would 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)");

outworking: inserir a descrição da imagem aqui

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);
                            }
                        }
                     }
             }
            }


        }
  • this generates an error:

  • Exception in thread "AWT-EventQueue-0" org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell 'Status dos Testes'!H3 at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:359)&#xA; at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:300)&#xA; at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:232)&#xA; at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCellValue(HSSFFormulaEvaluator.java:200)

  • AAAH! I got it using this yes! Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.