我阅读了Apache POI网站上关于如何将自定义函数注册到 FormulaEvaluator 的教程,我想用它来定义 POI 不提供支持的函数 MINVERSE。因此,首先我创建了一个定义 MINVERSE 的类(仅出于测试目的,我将 MINVERSE 定义为始终返回值 10)。所以这里是 MINVERSE.java:
package simpleboxapi;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
public class MINVERSE implements FreeRefFunction{
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
return new NumberEval(10);
}
}
之后我尝试了一些非常简单的方法:我创建了以下 excel 表:
A1 是给定常数,A2 是 A2=MINVERSE(A1)
这是我的主要课程代码:
package simpleboxapi;
import java.io.*;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.*;
public class SimpleBoxAPI {
static String fileName = "workbook.xls";
static Workbook wb;
private static double updateInputVal(String cell, double val) throws IOException, InvalidFormatException{
InputStream inp = new FileInputStream(fileName);
wb = WorkbookFactory.create(inp);
CellReference crInput = new CellReference(cell);
Sheet sheet = wb.getSheetAt(0);
Row rowInput = sheet.getRow(crInput.getRow());
Cell cellInput = rowInput.getCell(crInput.getCol());
cellInput.setCellValue(val);
FileOutputStream fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
fileOut.close();
double cellContents = cellInput.getNumericCellValue();
inp.close();
return cellContents;
}
private static void registerMINVERSE(){
String[] functionNames = {"MINVERSE"};
FreeRefFunction[] functionImpls = {new MINVERSE()};
UDFFinder udfs = new DefaultUDFFinder(functionNames, functionImpls);
UDFFinder udfToolpack = new AggregatingUDFFinder(udfs);
wb.addToolPack(udfToolpack);
}
public static void main(String[] args) throws Exception {
double updatedValue = updateInputVal("A1",55);
System.out.println(updatedValue);
registerMINVERSE();
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
CellReference cr = new CellReference("A2");
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(cr.getRow());
Cell cell = row.getCell(cr.getCol());
System.out.println(evaluator.evaluate(cell).getNumberValue());
}
}
但是,每当我尝试执行它时,我都会收到以下错误:
org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell 'new sheet'!A2
at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:356)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:297)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:229)
at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCellValue(HSSFFormulaEvaluator.java:354)
at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluate(HSSFFormulaEvaluator.java:185)
at simpleboxapi.SimpleBoxAPI.main(SimpleBoxAPI.java:56)
Caused by: org.apache.poi.ss.formula.eval.NotImplementedException: MINVERSE
at org.apache.poi.ss.formula.functions.NotImplementedFunction.evaluate(NotImplementedFunction.java:42)
at org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:132)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:491)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:287)
... 4 more
有什么建议么?提前非常感谢!