0

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

cell D30 =SUM(D22:D28) 
cell D31 =D30*0.12 
cell D32 =D31*0.02
cell D33 =D31*0.01
cell 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);

我错过了什么?

注意:只有在我打开生成的工作表并在每个单元格中按 Enter 后,才会计算公式!

4

1 回答 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()));
}
于 2013-03-05T09:18:21.027 回答