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.

ExcelAntWorkbookUtil.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.ss.excelant.util;
  16. import java.io.FileInputStream;
  17. import java.lang.reflect.InvocationTargetException;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  26. import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
  27. import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
  28. import org.apache.poi.ss.formula.udf.UDFFinder;
  29. import org.apache.poi.ss.usermodel.Cell;
  30. import org.apache.poi.ss.usermodel.CellValue;
  31. import org.apache.poi.ss.usermodel.FormulaError;
  32. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  33. import org.apache.poi.ss.usermodel.Row;
  34. import org.apache.poi.ss.usermodel.Sheet;
  35. import org.apache.poi.ss.usermodel.Workbook;
  36. import org.apache.poi.ss.usermodel.WorkbookFactory;
  37. import org.apache.poi.ss.util.CellReference;
  38. import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
  39. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  40. import org.apache.tools.ant.BuildException;
  41. import org.apache.tools.ant.Project;
  42. import org.apache.tools.ant.taskdefs.Typedef;
  43. /**
  44. * A general utility class that abstracts the POI details of loading the
  45. * workbook, accessing and updating cells.
  46. */
  47. public class ExcelAntWorkbookUtil extends Typedef {
  48. private String excelFileName;
  49. private Workbook workbook;
  50. private final Map<String, FreeRefFunction> xlsMacroList = new HashMap<>();
  51. /**
  52. * Constructs an instance using a String that contains the fully qualified
  53. * path of the Excel file. This constructor initializes a Workbook instance
  54. * based on that file name.
  55. *
  56. * @param fName The fully qualified path of the Excel file.
  57. * @throws BuildException If the workbook cannot be loaded.
  58. */
  59. protected ExcelAntWorkbookUtil(String fName) {
  60. excelFileName = fName;
  61. loadWorkbook();
  62. }
  63. /**
  64. * Constructs an instance based on a Workbook instance.
  65. *
  66. * @param wb The Workbook to use for this instance.
  67. */
  68. protected ExcelAntWorkbookUtil(Workbook wb) {
  69. workbook = wb;
  70. }
  71. /**
  72. * Loads the member variable workbook based on the fileName variable.
  73. * @return The opened Workbook-instance
  74. * @throws BuildException If the workbook cannot be loaded.
  75. */
  76. private Workbook loadWorkbook() {
  77. if (excelFileName == null) {
  78. throw new BuildException("fileName attribute must be set!", getLocation());
  79. }
  80. try {
  81. try (FileInputStream fis = new FileInputStream(excelFileName)) {
  82. workbook = WorkbookFactory.create(fis);
  83. }
  84. } catch(Exception e) {
  85. throw new BuildException("Cannot load file " + excelFileName
  86. + ". Make sure the path and file permissions are correct.", e);
  87. }
  88. return workbook;
  89. }
  90. /**
  91. * Used to add a UDF to the evaluator.
  92. * @param name The name of the function to add
  93. * @param clazzName The class which implements this function
  94. * @throws ClassNotFoundException if the class cannot be found
  95. * @throws InstantiationException if the class cannot be constructed
  96. * @throws IllegalAccessException if the constructor or the class is not accessible
  97. */
  98. public void addFunction(String name, String clazzName) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
  99. Class<?> clazzInst = Class.forName(clazzName);
  100. Object newInst = clazzInst.getDeclaredConstructor().newInstance();
  101. if(newInst instanceof FreeRefFunction) {
  102. addFunction(name, (FreeRefFunction)newInst);
  103. }
  104. }
  105. /**
  106. * Updates the internal HashMap of functions with instance and alias passed
  107. * in.
  108. *
  109. * @param name the name of the function to replace
  110. * @param func the function to use
  111. */
  112. protected void addFunction(String name, FreeRefFunction func) {
  113. xlsMacroList.put(name, func);
  114. }
  115. /**
  116. * returns a UDFFinder that contains all of the functions added.
  117. *
  118. * @return An instance of {@link UDFFinder} which can be used to
  119. * lookup functions
  120. */
  121. protected UDFFinder getFunctions() {
  122. String[] names = new String[xlsMacroList.size()];
  123. FreeRefFunction[] functions = new FreeRefFunction[xlsMacroList.size()];
  124. int x = 0;
  125. for(Map.Entry<String, FreeRefFunction> entry : xlsMacroList.entrySet()) {
  126. names[x] = entry.getKey();
  127. functions[x] = entry.getValue();
  128. }
  129. UDFFinder udff1 = new DefaultUDFFinder(names, functions);
  130. return new AggregatingUDFFinder(udff1);
  131. }
  132. /**
  133. * Returns a formula evaluator that is loaded with the functions that
  134. * have been supplied.
  135. *
  136. * @param fileName Specifies if XSSF or HSSF should be used for
  137. * the evaluator
  138. * @return A {@link FormulaEvaluator} constructed accordingly
  139. */
  140. protected FormulaEvaluator getEvaluator(String fileName) {
  141. FormulaEvaluator evaluator;
  142. if (fileName.endsWith(".xlsx")) {
  143. if(xlsMacroList.size() > 0) {
  144. evaluator = XSSFFormulaEvaluator.create((XSSFWorkbook) workbook,
  145. null,
  146. getFunctions());
  147. }
  148. evaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
  149. } else {
  150. if(xlsMacroList.size() > 0) {
  151. evaluator = HSSFFormulaEvaluator.create((HSSFWorkbook)workbook,
  152. null,
  153. getFunctions());
  154. }
  155. evaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
  156. }
  157. return evaluator;
  158. }
  159. /**
  160. * Returns the Workbook instance associated with this WorkbookUtil.
  161. */
  162. public Workbook getWorkbook() {
  163. return workbook;
  164. }
  165. /**
  166. * Returns the fileName that was used to initialize this instance. May
  167. * return null if the instance was constructed from a Workbook object.
  168. */
  169. public String getFileName() {
  170. return excelFileName;
  171. }
  172. /**
  173. * Returns the list of sheet names.
  174. */
  175. public List<String> getSheets() {
  176. ArrayList<String> sheets = new ArrayList<>();
  177. int sheetCount = workbook.getNumberOfSheets();
  178. for(int x=0; x<sheetCount; x++) {
  179. sheets.add(workbook.getSheetName(x));
  180. }
  181. return sheets;
  182. }
  183. /**
  184. * This method uses a String in standard Excel format (SheetName!CellId) to
  185. * locate the cell and set it to the value of the double in value.
  186. */
  187. public void setDoubleValue(String cellName, double value) {
  188. log("starting setCellValue()", Project.MSG_DEBUG);
  189. Cell cell = getCell(cellName);
  190. log("working on cell: " + cell, Project.MSG_DEBUG);
  191. cell.setCellValue(value);
  192. log("after cell.setCellValue()", Project.MSG_DEBUG);
  193. log("set cell " + cellName + " to value " + value, Project.MSG_DEBUG);
  194. }
  195. /**
  196. * Utility method for setting the value of a Cell with a String.
  197. */
  198. public void setStringValue(String cellName, String value) {
  199. Cell cell = getCell(cellName);
  200. cell.setCellValue(value);
  201. }
  202. /**
  203. * Utility method for setting the value of a Cell with a Formula.
  204. */
  205. public void setFormulaValue(String cellName, String formula) {
  206. Cell cell = getCell(cellName);
  207. cell.setCellFormula(formula);
  208. }
  209. /**
  210. * Utility method for setting the value of a Cell with a Date.
  211. */
  212. public void setDateValue(String cellName, Date date) {
  213. Cell cell = getCell(cellName);
  214. cell.setCellValue(date);
  215. }
  216. /**
  217. * Uses a String in standard Excel format (SheetName!CellId) to locate a
  218. * cell and evaluate it.
  219. */
  220. public ExcelAntEvaluationResult evaluateCell(String cellName, double expectedValue,
  221. double precision) {
  222. ExcelAntEvaluationResult evalResults;
  223. Cell cell = getCell(cellName);
  224. FormulaEvaluator evaluator = getEvaluator(excelFileName);
  225. CellValue resultOfEval = evaluator.evaluate(cell);
  226. if (resultOfEval.getErrorValue() == 0) {
  227. // the evaluation did not encounter errors
  228. double result = resultOfEval.getNumberValue();
  229. double delta = Math.abs(result - expectedValue);
  230. if (delta > precision) {
  231. evalResults = new ExcelAntEvaluationResult(false, false,
  232. resultOfEval.getNumberValue(),
  233. "Results was out of range based on precision " + " of "
  234. + precision + ". Delta was actually " + delta, delta, cellName);
  235. } else {
  236. evalResults = new ExcelAntEvaluationResult(false, true,
  237. resultOfEval.getNumberValue(),
  238. "Evaluation passed without error within in range.", delta, cellName);
  239. }
  240. } else {
  241. String errorMeaning;
  242. try {
  243. errorMeaning = FormulaError.forInt(resultOfEval.getErrorValue()).getString();
  244. } catch(IllegalArgumentException iae) {
  245. errorMeaning = "unknown error code: " + resultOfEval.getErrorValue();
  246. }
  247. evalResults = new ExcelAntEvaluationResult(true, false,
  248. resultOfEval.getNumberValue(),
  249. "Evaluation failed due to an evaluation error of "
  250. + resultOfEval.getErrorValue()
  251. + " which is "
  252. + errorMeaning, 0, cellName);
  253. }
  254. return evalResults;
  255. }
  256. /**
  257. * Returns a Cell as a String value.
  258. */
  259. public String getCellAsString(String cellName) {
  260. Cell cell = getCell(cellName);
  261. return cell.getStringCellValue();
  262. }
  263. /**
  264. * Returns the value of the Cell as a double.
  265. */
  266. public double getCellAsDouble(String cellName) {
  267. Cell cell = getCell(cellName);
  268. return cell.getNumericCellValue();
  269. }
  270. /**
  271. * Returns a cell reference based on a String in standard Excel format
  272. * (SheetName!CellId). This method will create a new cell if the
  273. * requested cell isn't initialized yet.
  274. */
  275. private Cell getCell(String cellName) {
  276. CellReference cellRef = new CellReference(cellName);
  277. String sheetName = cellRef.getSheetName();
  278. Sheet sheet = workbook.getSheet(sheetName);
  279. if(sheet == null) {
  280. throw new BuildException("Sheet not found: " + sheetName);
  281. }
  282. int rowIdx = cellRef.getRow();
  283. int colIdx = cellRef.getCol();
  284. Row row = sheet.getRow(rowIdx);
  285. if(row == null) {
  286. row = sheet.createRow(rowIdx);
  287. }
  288. Cell cell = row.getCell(colIdx);
  289. if(cell == null) {
  290. cell = row.createCell(colIdx);
  291. }
  292. return cell;
  293. }
  294. }