POI评估公式单元格IllegalArgumentException

时间:2014-01-19 04:07:15

标签: apache-poi

如果没有FormulaEvaluator,我可以将公式传递给excel,但是从公式单元格中检索值时会给我一个空值(使用cell.getRawValue())。

所以我尝试使用FormulaEvaluator API来评估公式。

这里我在XSSFCell的setFormula()方法中使用了VLOOKUP公式,FormulaEvaluator evaluate(formulaCell)方法抛出IllegalArgumentException

请帮助设置VLOOKUP公式并在执行公式后获取值。

FYI, 我正在使用poi-3.9-20121203.jar

以下是我的代码段和相应的例外,

FileInputStream file = new FileInputStream(new File("D:\\Excel_Vlookup\\temp1.xlsx"));
XSSFWorkbook  workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
XSSFCell formulaCell = null;
CellValue cellValue = null;

int count = sheet.getPhysicalNumberOfRows();

for (int i = 1; i < count; i++) {
formulaCell = sheet.getRow(i).createCell(7);
formulaCell.setCellFormula("VLOOKUP(B"+(i+1)+",'D:\\Excel_Vlookup\\[temp2.xlsx]Sheet1'!A:C,3,0)");
cellValue = evaluator.evaluate(formulaCell);
}
file.close();

如下所示,

java.lang.IllegalArgumentException: Invalid sheetIndex: -1.
at org.apache.poi.ss.formula.SheetRefEvaluator.<init>(SheetRefEvaluator.java:35)
at org.apache.poi.ss.formula.OperationEvaluationContext.createExternSheetRefEvaluator(OperationEvaluationContext.java:97)
at org.apache.poi.ss.formula.OperationEvaluationContext.getArea3DEval(OperationEvaluationContext.java:259)
at org.apache.poi.ss.formula.WorkbookEvaluator.getEvalForPtg(WorkbookEvaluator.java:576)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:458)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:279)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:221)
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateFormulaCellValue(XSSFFormulaEvaluator.java:257)
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluate(XSSFFormulaEvaluator.java:116)
at com.cgi.MyExcelFormula.writeFormula(MyExcelFormula.java:31)
at com.cgi.MyExcelFormula.main(MyExcelFormula.java:52)

0 个答案:

没有答案