我在保存新的excel文件时遇到问题。我希望当它被保存时,公式会自动计算,但目前它只是在excel文件中返回一个字符串。这个公式是正确的。我不知道如何让FormulaEvaluator正常工作。
下面是我输入返回字符串的公式的地方:
dataRow1.createCell((short)5).setCellValue("=VLOOKUP(A"+rowCountVlookup+",'C:\\Users\\Admin\\Documents\\JCreator LE\\MyProjects\\WordCount\\classes\\[Pricing.xlsx]Sheet1'!$B$3:$E$41,4, FALSE)*E"+rowCountVlookup+"");任何帮助都将不胜感激。
发布于 2012-06-06 21:54:47
我使用这个代码来计算一个公式
//I use an instance of the workbook for the Excel workbook I'm working at the moment
Workbook wbook;
private CellValue formulaEvaluation(Cell cell) {
FormulaEvaluator formulaEval = wbook.getCreationHelper().createFormulaEvaluator();
return formulaEval.evaluate(cell);
}
public Double obtieneObjetoNumericoCelda(Cell cell) {
Double dblValue = null;
if (cell != null) {
switch(cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
dblValue = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
CellValue objCellValue = formulaEvaluation(cell);
if (objCellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
dblValue = objCellValue.getNumberValue();
}
break;
}
}
return dblValor;
}发布于 2012-06-07 18:22:37
我最终设法解决了这个问题。
String strFormula = "ROUND((VLOOKUP(A"+rowCountVlookup+",'C:\\Users\\Admin\\Desktop\\[pricing.xlsx]Sheet1'!$B$3:$E$41,4, FALSE)*E"+rowCountVlookup+"),2)";
dataRow1.createCell((short)5).setCellType(Cell.CELL_TYPE_FORMULA);
dataRow1.createCell((short)5).setCellFormula(strFormula);https://stackoverflow.com/questions/10915475
复制相似问题