首页 文章

POI公式评估程序无法正常工作

提问于
浏览
0

我使用poi-ooxml 3.9从我存储在db中的excel模板生成发票报告 . 我在表格的末尾有公式,它将进行总和和其他税收计算 .

单元格D30 = SUM(D22:D28)
单元格D31 = D30 * 0.12
单元格D32 = D31 * 0.02
细胞D33 = D31 * 0.01
单元格D35 = SUM(D30:D33)

在我将工作表写入新文件之前,我会评估公式,但没有任何效果 . 下面的代码用于评估公式 . 每当我将值写入单元格或仅在操作结束时,是否需要评估公式?

String formulaCells[] = { "D30", "D31", "D32", "D33", "D35" };
FormulaEvaluator formulaEvaluator = workBook.getCreationHelper()
        .createFormulaEvaluator();
Sheet sheet = workBook.getSheetAt(0);
for (String cellRef : formulaCells) {
CellReference ref = new CellReference(cellRef);
Row actualRow = sheet.getRow(ref.getRow());
Cell cell = actualRow.getCell(ref.getCol());
if (cell != null) {
// here I evaluate the formula     
formulaEvaluator.evaluateFormulaCell(cell);

我错过了什么?

注意:只有在我打开生成的工作表并按下每个单元格中的输入后,才会评估公式!

1 回答

  • 1

    您确定您的单元格是否已添加为数字?

    如果没有,尝试做这样的事情:

    if(type.equals("String")) {
       cell.setCellValue (value.toString());
    } else if (type.equals("int")) {
       cell.setCellValue(Integer.parseInt(value.toString()));
    } else if (type.equals("double")) {
       cell.setCellValue(Double.parseDouble(value.toString()));
    }
    

相关问题