You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserDefinedFunctionExample.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.ss.formula;
  16. import java.io.File;
  17. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  18. import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
  19. import org.apache.poi.ss.formula.udf.UDFFinder;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.CellValue;
  22. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.ss.usermodel.Sheet;
  25. import org.apache.poi.ss.usermodel.Workbook;
  26. import org.apache.poi.ss.usermodel.WorkbookFactory;
  27. import org.apache.poi.ss.util.CellReference;
  28. /**
  29. * An example class of how to invoke a User Defined Function for a given
  30. * XLS instance using POI's UDFFinder implementation.
  31. */
  32. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  33. public final class UserDefinedFunctionExample {
  34. private UserDefinedFunctionExample() {}
  35. public static void main(String[] args ) throws Exception {
  36. if( args.length != 2 ) {
  37. // e.g. src/examples/src/org/apache/poi/ss/examples/formula/mortgage-calculation.xls Sheet1!B4
  38. System.out.println( "usage: UserDefinedFunctionExample fileName cellId" ) ;
  39. return;
  40. }
  41. System.out.println( "fileName: " + args[0] ) ;
  42. System.out.println( "cell: " + args[1] ) ;
  43. File workbookFile = new File( args[0] ) ;
  44. try (Workbook workbook = WorkbookFactory.create(workbookFile, null, true)) {
  45. String[] functionNames = {"calculatePayment"};
  46. FreeRefFunction[] functionImpls = {new CalculateMortgage()};
  47. UDFFinder udfToolpack = new DefaultUDFFinder(functionNames, functionImpls);
  48. // register the user-defined function in the workbook
  49. workbook.addToolPack(udfToolpack);
  50. FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
  51. CellReference cr = new CellReference(args[1]);
  52. String sheetName = cr.getSheetName();
  53. Sheet sheet = workbook.getSheet(sheetName);
  54. int rowIdx = cr.getRow();
  55. int colIdx = cr.getCol();
  56. Row row = sheet.getRow(rowIdx);
  57. Cell cell = row.getCell(colIdx);
  58. CellValue value = evaluator.evaluate(cell);
  59. System.out.println("returns value: " + value);
  60. }
  61. }
  62. }