Home » excel » java – Jexcel Formula Calculation Error

java – Jexcel Formula Calculation Error

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have created a worksheet, out.xls which had cell D6=D5*2 and D5 set to 1. My issue is that when I plug a value into D5 in jxl, D6 never calculates. D6 simply holds on the value it initially calculated when I plugged in 1 to D5 in excel.

note: I have a much larger programming problem that I am trying to tackle, this is just a very scaled down version to reduce error.

This is my first time ever use Jexcel and I only just learned java this last year, so any help would be appreciated. I spent 6 hours yesterday trying to find an answer on the web, but to no avail.

the output is attached below the code
Code: (left out the main and imports)

        WorkbookSettings custom= new WorkbookSettings();
        custom.setRationalization(true);
        custom.setRefreshAll(true);
        custom.setUseTemporaryFileDuringWrite(true);
        Workbook workbook = Workbook.getWorkbook(new File("out.xls"),custom); 


        WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"), workbook); 

        WritableSheet sheet2 = copy.getSheet(0); 
        SheetSettings customsheetsettings=new SheetSettings(sheet2);
        customsheetsettings.setAutomaticFormulaCalculation(true); 
        Number number = new Number(3, 4, 3);
        sheet2.addCell(number); 


        copy.write();
        copy.close(); 
        workbook.close();


        Workbook workbook2 = Workbook.getWorkbook(new File("output.xls")); 
        Sheet sheet=workbook2.getSheet(0);


        System.out.println("D5:"+sheet.getCell(3,4).getContents());
        FormulaCell formula5=(FormulaCell) sheet.getCell(3,5);
        System.out.println("Formula:"+formula5.getFormula());
        System.out.println("D6:"+formula5.getContents()); 

        NumberFormulaCell nfc=(NumberFormulaCell)sheet.getCell(3, 5); 
        System.out.println(nfc.getValue());  
        workbook2.close();

output:

D5:3
Formula:D5*2.0
D6:2
2.0
How to&Answers:

JExcel only allows to read or write Excel files, and give a Java representation of the content of an Excel file. It will not substitute for Excel.

When you add a formula, this formula will be computed, but only in Excel, when you open the generated file in Excel. The cell is not computed by JExcel.

Answer:

If anyone else wants to know, POI does that using the FormulaEvaluator class.

Att