Java POI TREND功能不受支持

时间:2017-11-13 19:57:08

标签: apache-poi

尝试评估功能为TREND的电子表格

我收到此错误消息

  

org.apache.poi.ss.formula.eval.NotImplementedFunctionException:TREND

所以我按照https://poi.apache.org/spreadsheet/user-defined-functions.html

的说明进行操作

但我仍然得到同样的错误。

我当前的代码看起来像

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.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.IOException;

public class TestMain {
    public static void main(String[] args) throws IOException, InvalidFormatException {
        Workbook wb = new XSSFWorkbook(new File("my-file.xlsx"));
        String[] functionNames = { "TREND" } ;
        FreeRefFunction[] functionImpls = { new TrendFunction() } ;

        UDFFinder udfs = new DefaultUDFFinder( functionNames, functionImpls ) ;
        UDFFinder udfToolpack = new AggregatingUDFFinder( udfs ) ;

        wb.addToolPack(udfToolpack);
        FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
        formulaEvaluator.clearAllCachedResultValues();
        formulaEvaluator.evaluateAll();

    }
}

TrendFunction目前是模拟

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 ;

/**
 * A simple function to calculate principal and interest.
 *
 * @author Jon Svede
 *
 */
public class TrendFunction implements FreeRefFunction {

    public ValueEval evaluate( ValueEval[] args, OperationEvaluationContext ec ) {
        System.out.println("in TREND");
        return new NumberEval( 1 ) ;
    }

}

无法在输出中看到打印件..显然它没有被使用。

您可以从此处下载xlsx文件:https://filebin.ca/3hBZVrUKolfO

我得到的完整堆栈跟踪是

Exception in thread "main" org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell Sheet1!B18
    at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:347)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:288)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:220)
    at org.apache.poi.xssf.usermodel.BaseXSSFFormulaEvaluator.evaluateFormulaCellValue(BaseXSSFFormulaEvaluator.java:64)
    at org.apache.poi.ss.formula.BaseFormulaEvaluator.evaluateFormulaCellEnum(BaseFormulaEvaluator.java:192)
    at org.apache.poi.ss.formula.BaseFormulaEvaluator.evaluateAllFormulaCells(BaseFormulaEvaluator.java:267)
    at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateAll(XSSFFormulaEvaluator.java:92)
    at TestMain.main(TestMain.java:27)
Caused by: org.apache.poi.ss.formula.eval.NotImplementedFunctionException: TREND
    at org.apache.poi.ss.formula.functions.NotImplementedFunction.evaluate(NotImplementedFunction.java:40)
    at org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:132)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:514)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:278)
    ... 6 more

1 个答案:

答案 0 :(得分:1)

请注意,从4.01版开始,ApachePOI支持Excel Function TREND(请参见change log

请参见https://poi.apache.org/apidocs/4.0/org/apache/poi/ss/formula/functions/Trend.html