java POI XSSF FormulaEvaluator

我保存新的Excel文件时遇到问题。 我希望它在保存时公式计算自己,但目前它只是在excel文件中返回一个字符串。 公式是正确的。 我FormulaEvaluator地知道要让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+""); 

任何帮助将非常感激。

我使用此代码来评估公式

 //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; } 

我设法最终解决了这个问题。

 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);