2

I have an EXCEL Spreadsheet that has many computations some including recursive formula's (perfectly fine in excel which computes these with no problem).

I'm using version 3.8 of the Apache Poi API to open the existing spreadsheet, add updated values for the computations, save the spreadsheet and then read the results into my program.

All of this works except the in memory poi version of the spreadsheet does not perform the recursive calculation.

I'm calling the POI code to compute the entire workbook, each sheet and then each formula cell, but the cell formula never computes.

If I open the saved spreadsheet and compute it the formula computes the value accurately. I need to avoid having the user manually open the saved spreadsheet if possible.

Any insight in to this problem would be greatly appreciated.

Here is the code I have tried. It all runs with no error but does not compute the formula.

The formula is already in the spreadsheet an works fine so I'm trying to force the formula to re-compute.

I have tried all these variations. The code runs and executes but does not compute the formula

    wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
    /*
    //wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
    System.out.println(wb.getSheet("Rate Report").getRow(16).getCell(0).getStringCellValue());
    System.out.println(wb.getSheet("Rate Report").getRow(16).getCell(1).getNumericCellValue());
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

    //String wbsheets[] = {"Original","Main_DB","Parameters","Translator","Workers Comp.","Rate Report","Notes Fields"};
    String wbsheets[] = {"Notes Fields","Translator","Parameters","Rate Report"};
    for(String sheetName : wbsheets) {
    Sheet sheet = wb.getSheet(sheetName);
    System.out.println("processing sheet: " + sheet.getSheetName());
    for(Row r : sheet) {
    for(Cell c : r) {
    if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
    System.out.println("Recalcing: "+r.getRowNum()+c.getColumnIndex() + "in "+ sheet.getSheetName());
    evaluator.evaluateFormulaCell(c);    
    }
    }
    }
    }

XSSFFormulaEvaluator.evaluateAllFormulaCells((XSSFWorkbook) wb);
4

0 回答 0