From: Nick Burch Date: Wed, 9 Jan 2008 20:37:06 +0000 (+0000) Subject: Update documentation, and add section on whole-workbook recalculating X-Git-Tag: REL_3_0_3_BETA1~194 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=80cf60500a3de5ebcddc6958affe59a6f043fe3c;p=poi.git Update documentation, and add section on whole-workbook recalculating git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610553 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/hssf/eval.xml b/src/documentation/content/xdocs/hssf/eval.xml index d697eb082b..197391b381 100644 --- a/src/documentation/content/xdocs/hssf/eval.xml +++ b/src/documentation/content/xdocs/hssf/eval.xml @@ -32,14 +32,17 @@ formulas in Excels sheets read-in, or created in POI. This document explains how to use the API to evaluate your formulas.

- This code currently lives the scratchpad area of the POI CVS repository. + This code currently lives the scratchpad area of the POI SVN repository. Ensure that you have the scratchpad jar or the scratchpad build area in your - classpath before experimenting with this code. + classpath before experimenting with this code. You are advised + to make use of a recent SVN checkout, as new functions are + being supported fairly frequently.
Status +

The code currently provides implementations for all the arithmatic operators. - It also provides implementations for approx. 20 built in + It also provides implementations for approx. 100 built in functions in Excel. The framework however makes is easy to add implementation of new functions. See the Formula evaluation development guide for details.

@@ -53,6 +56,7 @@

There are two ways in which you can use the HSSFFormulaEvalutator API.

Using HSSFFormulaEvaluator.<strong>evaluate</strong>(HSSFCell cell) + FileInputStream fis = new FileInputStream("c:/temp/test.xls"); HSSFWorkbook wb = new HSSFWorkbook(fis); @@ -96,8 +100,12 @@ switch (cellValue.getCellType()) {

-
Using HSSFFormulaEvaluator.<strong>evaluateInCell</strong>(HSSFCell cell) - +
Using HSSFFormulaEvaluator.<strong>evaluateInCell</strong>(HSSFCell cell) + +

evaluateInCell(HSSFCell cell) will check to + see if the supplied cell is a formula cell. If it isn't, + then no changes will be made to it. If it is, then the + formula is evaluated, and the new value saved into the cell.

FileInputStream fis = new FileInputStream("/somepath/test.xls"); HSSFWorkbook wb = new HSSFWorkbook(fis); @@ -132,12 +140,36 @@ if (cell!=null) { break; } } - + +
+
Re-calculating all formulas in a Workbook + + +FileInputStream fis = new FileInputStream("/somepath/test.xls"); +HSSFWorkbook wb = new HSSFWorkbook(fis); +for(int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) { + HSSFSheet sheet = wb.getSheetAt(sheetNum); + HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet, wb); -
+ for(Iterator rit = s.rowIterator(); rit.hasNext();) { + HSSFRow r = (HSSFRow)rit.next(); + evaluator.setCurrentRow(r); + + for(Iterator cit = r.cellIterator(); cit.hasNext();) { + HSSFCell c = (HSSFCell)cit.next(); + if(c.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { + evaluator.evaluateInCell(c); + } + } + } +} +wb.write(new FileOutputStream("/somepath/changed.xls")); + +
Performance Notes +
  • Generally you should have to create only one HSSFFormulaEvaluator instance per sheet, but there really is no overhead in creating