FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
CellValue evalCellValue = evaluator.evaluate(cell);
if(evalCellValue.getCellType() == Cell.CELL_TYPE_STRING)
{
res = evalCellValue.getStringValue();
}
else if(evalCellValue.getCellType()==Cell.CELL_TYPE_BOOLEAN)
{
res = Boolean.toString(evalCellValue.getBooleanValue());
}
if(evalCellValue.getCellType()==Cell.CELL_TYPE_NUMERIC)
{
res = Double.toString(evalCellValue.getNumberValue());
}
我想获取单元格的评估值,但它给出了#DIV/0!错误 单元格上的公式:
="IF($C$31<SUM($C$24:$C$25),$C$31*C24/SUM($C$24:$C$25),C24)"
地点:
- c-24 值为 0.00
- c-25 值为 0.00
- c-31 为 0.00
因此数值公式为:
"if(0<SUM(0:0),0*1/SUM(0:0),0)"
看起来无法计算公式中的 if 条件。 我该如何解决?
最佳答案
无法使用 apache poi 4.0.0.
重现此问题.
有以下 Excel:
A1
中的公式是 =IF($C$31<SUM($C$24:$C$25),$C$31*C24/SUM($C$24:$C$25),C24)
.
使用此代码:
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
class ExcelFormulaEvaluatorIFDIV0 {
public static void main(String[] args) throws Exception {
Workbook workbook = WorkbookFactory.create(new FileInputStream("SAMPLE.xlsx"));
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheetAt(0);
Cell cell = sheet.getRow(0).getCell(0);
CellValue cellvalue = evaluator.evaluate(cell);
System.out.println(cell.getAddress() + ":" + cell + " evaluates to:" + cellvalue);
workbook.close();
}
}
产品:
axel@arichter:~/Dokumente/JAVA/poi/poi-4.0.0$ java -cp .:./*:./lib/*:./ooxml-lib/* ExcelFormulaEvaluatorIFDIV0
A1:IF($C$31<SUM($C$24:$C$25),$C$31*C24/SUM($C$24:$C$25),C24) evaluates to:org.apache.poi.ss.usermodel.CellValue [0.0]
所以它的评价很好。
关于java - 无法使用 poi Java 计算多个工作表之间的公式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52718355/