Example usage for org.apache.poi.ss.formula.udf DefaultUDFFinder DefaultUDFFinder

List of usage examples for org.apache.poi.ss.formula.udf DefaultUDFFinder DefaultUDFFinder

Introduction

In this page you can find the example usage for org.apache.poi.ss.formula.udf DefaultUDFFinder DefaultUDFFinder.

Prototype

public DefaultUDFFinder(String[] functionNames, FreeRefFunction[] functionImpls) 

Source Link

Usage

From source file:com.dataart.spreadsheetanalytics.engine.Functions.java

License:Apache License

/**
 * Does create a new instance of {@link UDFFinder} based on Set of CustomFunction provided.
 * If you extend {@link Functions} class and replace/add {@link #fs} please also replace/add {@link #poifs}.
 * UDFFinder instance is needed for some CustomFunction to do evaluation in POI.
 *//*  w  w w .  ja  va  2 s. c  om*/
protected static UDFFinder loadPoiCustomFunctions(Map<String, Class<? extends ICustomFunction>> fs) {
    List<String> names = new ArrayList<>(fs.size());
    List<ICustomFunction> funcs = new ArrayList<>(fs.size());

    for (Entry<String, Class<? extends ICustomFunction>> en : fs.entrySet()) {

        try {
            names.add(en.getKey());
            funcs.add(en.getValue().newInstance());
        } catch (Exception e) {
            log.error(String.format("Cannot create instance of CustomFunction %s", en.getKey()), e);
        }
    }

    return names.isEmpty() ? null
            : new AggregatingUDFFinder(new DefaultUDFFinder(names.toArray(new String[names.size()]),
                    funcs.toArray(new ICustomFunction[funcs.size()])));
}

From source file:com.wantdo.stat.excel.poi_src.formula.UserDefinedFunctionExample.java

License:Apache License

public static void main(String[] args) {

    if (args.length != 2) {
        System.out.println("usage: UserDefinedFunctionExample fileName cellId");
        return;//from  w  w  w  .j  av a2 s.c  om
    }

    System.out.println("fileName: " + args[0]);
    System.out.println("cell: " + args[1]);

    File workbookFile = new File(args[0]);

    try {
        FileInputStream fis = new FileInputStream(workbookFile);
        Workbook workbook = WorkbookFactory.create(fis);
        fis.close();

        String[] functionNames = { "calculatePayment" };
        FreeRefFunction[] functionImpls = { new CalculateMortgage() };

        UDFFinder udfToolpack = new DefaultUDFFinder(functionNames, functionImpls);

        // register the user-defined function in the workbook
        workbook.addToolPack(udfToolpack);

        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();

        CellReference cr = new CellReference(args[1]);
        String sheetName = cr.getSheetName();
        Sheet sheet = workbook.getSheet(sheetName);
        int rowIdx = cr.getRow();
        int colIdx = cr.getCol();
        Row row = sheet.getRow(rowIdx);
        Cell cell = row.getCell(colIdx);

        CellValue value = evaluator.evaluate(cell);

        System.out.println("returns value: " + value);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.enerko.reports2.engine.ReportEngine.java

License:Apache License

private UDFFinder createCustomFunctions() {
    UDFFinder rv = null;/* ww w  . j av a2  s . c  o m*/

    if (this.customFunctions.size() > 0) {
        String[] names = new String[this.customFunctions.size()];
        FreeRefFunction[] implementations = new FreeRefFunction[this.customFunctions.size()];
        int i = 0;
        for (Map.Entry<String, FreeRefFunction> entry : this.customFunctions.entrySet()) {
            names[i] = entry.getKey();
            implementations[i] = entry.getValue();
            ++i;
        }
        rv = new AggregatingUDFFinder(new DefaultUDFFinder(names, implementations));
    }

    return rv;
}