Java Apache POI计算公式未给出预期结果

Java Apache POI计算公式未给出预期结果,java,android,apache-poi,Java,Android,Apache Poi,在这段代码中,我希望得到结果并将其放入java变量中,以便稍后使用,但日志显示了其他内容 private void test() { Workbook wb = new HSSFWorkbook(); try { //OutputStream os = new FileOutputStream("Javatpoint.xls"); Sheet sheet = wb.createSheet("sheet"); Cell cell1 =

在这段代码中,我希望得到结果并将其放入java变量中,以便稍后使用,但日志显示了其他内容

private void test() {
    Workbook wb = new HSSFWorkbook();
    try {
        //OutputStream os = new FileOutputStream("Javatpoint.xls");
        Sheet sheet = wb.createSheet("sheet");
        Cell cell1 = sheet.createRow(0).createCell(0);//A1
        Cell cell2 = sheet.createRow(0).createCell(1);//B1
        Cell cell3 = sheet.createRow(0).createCell(2);//C1
        Cell cell4 = sheet.createRow(0).createCell(3);//D1
        Cell cell5 = sheet.createRow(0).createCell(4);//E1

        cell1.setCellValue(5);//A1
        cell2.setCellValue(5);//B1
        cell3.setCellValue("A + ");//C1
        cell4.setCellValue("B is: ");//D1
        cell5.setCellFormula("A1+B1");//D1


        wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
        sheet.setForceFormulaRecalculation(true);

        Log.i("loginf", String.valueOf(cell1.getNumericCellValue()));
        Log.i("loginf", String.valueOf(cell2.getNumericCellValue()));
        Log.i("loginf", cell3.getStringCellValue());
        Log.i("loginf", cell4.getStringCellValue());
        Log.i("loginf", "result: " + String.valueOf(cell5.getNumericCellValue()));

        //Log.i("loginf" , cell.getAddress().formatAsString());
        //wb.write(os);
    } catch (Exception e) {
        Log.i("loginf", e.getCause().getMessage());
    }
}
结果是:

5.0 5.0 A + B is: result: 0.0 but A1+B1 should be 10 while is 0 5 5 A+ B是: 结果:0.0 但A1+B1应为10,而值为0
您不应该创建行5次。只需将单元格初始化更改为以下内容即可获得所需结果:

Row row = sheet.createRow(0);
Cell cell1 = row.createCell(0);//A1
Cell cell2 = row.createCell(1);//B1
Cell cell3 = row.createCell(2);//C1
Cell cell4 = row.createCell(3);//D1
Cell cell5 = row.createCell(4);//E1

您不应该创建行5次。只需将单元格初始化更改为以下内容即可获得所需结果:

Row row = sheet.createRow(0);
Cell cell1 = row.createCell(0);//A1
Cell cell2 = row.createCell(1);//B1
Cell cell3 = row.createCell(2);//C1
Cell cell4 = row.createCell(3);//D1
Cell cell5 = row.createCell(4);//E1

Log.i(“loginf”,“result:”+String.valueOf(cell5.getNumericCellValue());在日志结果中,result关键字在哪里?据我所知,您应该首先设置单元格类型
cell.setCellType(HSSFCell.cell\u type\u公式)我也认为这个公式不正确,试试这个
cell.setCellFormula(“SUM(A1:B1)”
您是否尝试过将
A1+B1
更改为
SUM(A1:B1)
?A1+B1和SUM(A1:B1)返回相同的结果。HSSF工作簿与其他simular有什么不同?Log.i(“loginf”,“result:+String.valueOf(cell5.getNumericCellValue());在日志结果中,result关键字在哪里?据我所知,您应该首先设置单元格类型
cell.setCellType(HSSFCell.cell\u type\u公式)我也认为这个公式不正确,试试这个
cell.setCellFormula(“SUM(A1:B1)”您是否尝试将
A1+B1
更改为
SUM(A1:B1)
?A1+B1和SUM(A1:B1)返回相同的结果。HSSF工作簿与其他模拟有什么不同?