Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SXSSFFormulaEvaluator.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.xssf.streaming;
  16. import org.apache.poi.ss.formula.EvaluationCell;
  17. import org.apache.poi.ss.formula.IStabilityClassifier;
  18. import org.apache.poi.ss.formula.WorkbookEvaluator;
  19. import org.apache.poi.ss.formula.udf.UDFFinder;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.CellType;
  22. import org.apache.poi.ss.usermodel.Row;
  23. import org.apache.poi.ss.usermodel.Sheet;
  24. import org.apache.poi.util.POILogFactory;
  25. import org.apache.poi.util.POILogger;
  26. import org.apache.poi.xssf.usermodel.BaseXSSFFormulaEvaluator;
  27. /**
  28. * Streaming-specific Formula Evaluator, which is able to
  29. * lookup cells within the current Window.
  30. */
  31. public final class SXSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
  32. private static POILogger logger = POILogFactory.getLogger(SXSSFFormulaEvaluator.class);
  33. private SXSSFWorkbook wb;
  34. public SXSSFFormulaEvaluator(SXSSFWorkbook workbook) {
  35. this(workbook, null, null);
  36. }
  37. private SXSSFFormulaEvaluator(SXSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
  38. this(workbook, new WorkbookEvaluator(SXSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder));
  39. }
  40. private SXSSFFormulaEvaluator(SXSSFWorkbook workbook, WorkbookEvaluator bookEvaluator) {
  41. super(bookEvaluator);
  42. this.wb = workbook;
  43. }
  44. /**
  45. * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
  46. * for the (conservative) assumption that any cell may have its definition changed after
  47. * evaluation begins.
  48. * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
  49. */
  50. public static SXSSFFormulaEvaluator create(SXSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
  51. return new SXSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
  52. }
  53. /**
  54. * Turns a SXSSFCell into a SXSSFEvaluationCell
  55. */
  56. @Override
  57. protected EvaluationCell toEvaluationCell(Cell cell) {
  58. if (!(cell instanceof SXSSFCell)){
  59. throw new IllegalArgumentException("Unexpected type of cell: " + cell.getClass() + "." +
  60. " Only SXSSFCells can be evaluated.");
  61. }
  62. return new SXSSFEvaluationCell((SXSSFCell)cell);
  63. }
  64. @Override
  65. public SXSSFCell evaluateInCell(Cell cell) {
  66. return (SXSSFCell) super.evaluateInCell(cell);
  67. }
  68. /**
  69. * For active worksheets only, will loop over rows and
  70. * cells, evaluating formula cells there.
  71. * If formula cells are outside the window for that sheet,
  72. * it can either skip them silently, or give an exception
  73. */
  74. public static void evaluateAllFormulaCells(SXSSFWorkbook wb, boolean skipOutOfWindow) {
  75. SXSSFFormulaEvaluator eval = new SXSSFFormulaEvaluator(wb);
  76. // Check they're all available
  77. for (Sheet sheet : wb) {
  78. if (((SXSSFSheet)sheet).areAllRowsFlushed()) {
  79. throw new SheetsFlushedException();
  80. }
  81. }
  82. // Process the sheets as best we can
  83. for (Sheet sheet : wb) {
  84. // Check if any rows have already been flushed out
  85. int lastFlushedRowNum = ((SXSSFSheet) sheet).getLastFlushedRowNum();
  86. if (lastFlushedRowNum > -1) {
  87. if (! skipOutOfWindow) throw new RowFlushedException(0);
  88. logger.log(POILogger.INFO, "Rows up to " + lastFlushedRowNum + " have already been flushed, skipping");
  89. }
  90. // Evaluate what we have
  91. for (Row r : sheet) {
  92. for (Cell c : r) {
  93. if (c.getCellTypeEnum() == CellType.FORMULA) {
  94. eval.evaluateFormulaCellEnum(c);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * Loops over rows and cells, evaluating formula cells there.
  102. * If any sheets are inactive, or any cells outside of the window,
  103. * will give an Exception.
  104. * For SXSSF, you generally don't want to use this method, instead
  105. * evaluate your formulas as you go before they leave the window.
  106. */
  107. public void evaluateAll() {
  108. // Have the evaluation done, with exceptions
  109. evaluateAllFormulaCells(wb, false);
  110. }
  111. public static class SheetsFlushedException extends IllegalStateException {
  112. protected SheetsFlushedException() {
  113. super("One or more sheets have been flushed, cannot evaluate all cells");
  114. }
  115. }
  116. public static class RowFlushedException extends IllegalStateException {
  117. protected RowFlushedException(int rowNum) {
  118. super("Row " + rowNum + " has been flushed, cannot evaluate all cells");
  119. }
  120. }
  121. }