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

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