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.

eval.xml 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Copyright (C) 2005 The Apache Software Foundation. All rights reserved. -->
  3. <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.1//EN" "../dtd/document-v11.dtd">
  4. <document>
  5. <header>
  6. <title>Formula Evaluation</title>
  7. <authors>
  8. <person email="amoweb@yahoo.com" name="Amol Deshmukh" id="AD"/>
  9. </authors>
  10. </header>
  11. <body>
  12. <section><title>Introduction</title>
  13. <p>The POI formula evaluation code enables you to calculate the result of
  14. formulas in Excels sheets read-in, or created in POI. This document explains
  15. how to use the API to evaluate your formulas.
  16. </p>
  17. <warning> This code currently lives in Bugzilla as
  18. <link href="http://issues.apache.org/bugzilla/show_bug.cgi?id=34828">
  19. bug 34828 </link>. It is expected to land in POI CVS in the scratchpad
  20. area soon.
  21. </warning>
  22. </section>
  23. <section><title>Status</title>
  24. <p> The code currently provides implementations for all the arithmatic operators.
  25. It also provides implementations for about 30 built in
  26. functions in Excel. The framework however makes is easy to add
  27. implementation of new functions. See the <link href="eval-devguide.html"> Formula
  28. evaluation development guide</link> for details. </p>
  29. <p> Note that user-defined functions are not supported, and is not likely to done
  30. any time soon... at least, not till there is a VB implementation in Java!
  31. </p>
  32. </section>
  33. <section><title>User API How-TO</title>
  34. <p>The following code demonstrates how to use the HSSFFormulaEvaluator
  35. in the context of other POI excel reading code.
  36. </p>
  37. <p>There are two ways in which you can use the HSSFFormulaEvalutator API.</p>
  38. <section><title>Using HSSFFormulaEvaluator.<strong>evaluate</strong>(HSSFCell cell)</title>
  39. <source>
  40. FileInputStream fis = new FileInputStream("c:/temp/test.xls");
  41. HSSFWorkbook wb = new HSSFWorkbook(fis);
  42. HSSFSheet sheet = wb.getSheetAt(0);
  43. HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet, wb);
  44. // suppose your formula is in B3
  45. CellReference cellReference = new CellReference("B3");
  46. HSSFRow row = sheet.getRow(cellReference.getRow());
  47. HSSFCell cell = row.getCell(cellReference.getCol());
  48. String formulaString = c.getCellFormula();
  49. HSSFFormulaEvaluator.CellValue cellValue =
  50. evaluator.evaluate(formulaString);
  51. switch (cellValue.getCellType()) {
  52. case HSSFCell.CELL_TYPE_BOOLEAN:
  53. System.out.println(cellValue.getBooleanCellValue());
  54. break;
  55. case HSSFCell.CELL_TYPE_NUMERIC:
  56. System.out.println(cellValue.getNumberCellValue());
  57. break;
  58. case HSSFCell.CELL_TYPE_STRING:
  59. System.out.println(cellValue.getStringCellValue());
  60. break;
  61. case HSSFCell.CELL_TYPE_BLANK:
  62. break;
  63. case HSSFCell.CELL_TYPE_ERROR:
  64. break;
  65. // CELL_TYPE_FORMULA will never happen
  66. case HSSFCell.CELL_TYPE_FORMULA:
  67. break;
  68. }
  69. </source>
  70. <p>Thus using the retrieved value (of type
  71. HSSFFormulaEvaluator.CellValue - a nested class) returned
  72. by HSSFFormulaEvaluator is similar to using a HSSFCell object
  73. containing the value of the formula evaluation. CellValue is
  74. a simple value object and does not maintain reference
  75. to the original cell.
  76. </p>
  77. </section>
  78. <section><title>Using HSSFFormulaEvaluator.<strong>evaluateInCell</strong>(HSSFCell cell)
  79. </title>
  80. <source>
  81. FileInputStream fis = new FileInputStream("c:/temp/test.xls");
  82. HSSFWorkbook wb = new HSSFWorkbook(fis);
  83. HSSFSheet sheet = wb.getSheetAt(0);
  84. HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet, wb);
  85. // suppose your formula is in B3
  86. CellReference cellReference = new CellReference("B3");
  87. HSSFRow row = sheet.getRow(cellReference.getRow());
  88. HSSFCell cell = row.getCell(cellReference.getCol());
  89. String formulaString = c.getCellFormula();
  90. if (cell!=null) {
  91. switch (<strong>evaluator.evaluateInCell</strong>(cell).getCellType()) {
  92. case HSSFCell.CELL_TYPE_BOOLEAN:
  93. System.out.println(cell.getBooleanCellValue());
  94. break;
  95. case HSSFCell.CELL_TYPE_NUMERIC:
  96. System.out.println(cell.getNumberCellValue());
  97. break;
  98. case HSSFCell.CELL_TYPE_STRING:
  99. System.out.println(cell.getStringCellValue());
  100. break;
  101. case HSSFCell.CELL_TYPE_BLANK:
  102. break;
  103. case HSSFCell.CELL_TYPE_ERROR:
  104. System.out.println(cell.getErrorCellValue());
  105. break;
  106. // CELL_TYPE_FORMULA will never occur
  107. case HSSFCell.CELL_TYPE_FORMULA:
  108. break;
  109. }
  110. }
  111. </source>
  112. </section>
  113. </section>
  114. <section><title></title>
  115. </section>
  116. <section><title>Performance Notes</title>
  117. <ul>
  118. <li>Generally you should have to create only one HSSFFormulaEvaluator
  119. instance per sheet, but there really is no overhead in creating
  120. multiple HSSFFormulaEvaluators per sheet other than that of the
  121. HSSFFormulaEvaluator object creation.
  122. </li>
  123. <li>Also note that HSSFFormulaEvaluator maintains a reference to
  124. the sheet and workbook, so ensure that the evaluator instance
  125. is available for garbage collection when you are done with it
  126. (in other words don't maintain long lived reference to
  127. HSSFFormulaEvaluator if you don't really need to - unless
  128. all references to the sheet and workbook are removed, these
  129. don't get garbage collected and continue to occupy potentially
  130. large amounts of memory).
  131. </li>
  132. <li>CellValue instances however do not maintain reference to the
  133. HSSFCell or the sheet or workbook, so these can be long-lived
  134. objects without any adverse effect on performance.
  135. </li>
  136. </ul>
  137. </section>
  138. </body>
  139. </document>