PageRenderTime 127ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/manticore/report/FormulaTest.java

https://bitbucket.org/manticoretrader/etltools
Java | 90 lines | 60 code | 8 blank | 22 comment | 8 complexity | 7b68c9964bb208d63c4c92c259779657 MD5 | raw file
  1. /*
  2. * Copyright (C) 2014 Andreas Reichel <andreas@manticore-projects.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package com.manticore.report;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.InputStream;
  21. import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
  22. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  23. import org.apache.poi.ss.formula.FormulaParsingWorkbook;
  24. import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  27. import org.apache.poi.ss.usermodel.Name;
  28. import org.apache.poi.ss.usermodel.Row;
  29. import org.apache.poi.ss.usermodel.Sheet;
  30. import org.apache.poi.ss.usermodel.Workbook;
  31. import org.apache.poi.ss.usermodel.WorkbookFactory;
  32. import org.apache.poi.ss.util.AreaReference;
  33. import org.apache.poi.ss.util.CellReference;
  34. import org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook;
  35. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  36. /**
  37. *
  38. * @author Andreas Reichel <andreas@manticore-projects.com>
  39. */
  40. public class FormulaTest {
  41. public static void main(String[] args) throws Exception {
  42. for (int k=0; k<10000; k++) {
  43. File f = new File("/tmp/test.xls");
  44. InputStream inputStream = new FileInputStream(f);
  45. final Workbook workbook = WorkbookFactory.create(inputStream);
  46. final FormulaParsingWorkbook parsingWorkbook = (workbook instanceof HSSFWorkbook)
  47. ? HSSFEvaluationWorkbook.create((HSSFWorkbook) workbook)
  48. : XSSFEvaluationWorkbook.create((XSSFWorkbook) workbook);
  49. final FormulaRenderingWorkbook renderingWorkbook = (workbook instanceof HSSFWorkbook)
  50. ? HSSFEvaluationWorkbook.create((HSSFWorkbook) workbook)
  51. : XSSFEvaluationWorkbook.create((XSSFWorkbook) workbook);
  52. Sheet sheet1 = workbook.getSheetAt(0);
  53. for (int i=1; i<8; i++) {
  54. sheet1.getRow(i).getCell(2).setCellValue(Math.random());
  55. }
  56. FormulaEvaluator evaluator = workbook.getCreationHelper()
  57. .createFormulaEvaluator();
  58. for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
  59. Sheet sheet = workbook.getSheetAt(sheetNum);
  60. for (Row r : sheet) {
  61. for (Cell c : r) {
  62. if (c.getCellType() == Cell.CELL_TYPE_FORMULA) {
  63. evaluator.evaluateFormulaCell(c);
  64. }
  65. }
  66. }
  67. }
  68. int namedCellIdx = workbook.getNameIndex("RatingResult");
  69. Name aNamedCell = workbook.getNameAt(namedCellIdx);
  70. // retrieve the cell at the named range and test its contents
  71. AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
  72. CellReference[] crefs = aref.getAllReferencedCells();
  73. for (int i = 0; i < crefs.length; i++) {
  74. Sheet sheet = workbook.getSheet(crefs[i].getSheetName());
  75. Row r = sheet.getRow(crefs[i].getRow());
  76. Cell cell = r.getCell(crefs[i].getCol());
  77. // extract the cell contents based on cell type etc.
  78. System.out.println(k + " Found RatingResult: " + cell.getNumericCellValue());
  79. }
  80. }
  81. }
  82. }