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.

BaseXSSFFormulaEvaluator.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.usermodel;
  16. import org.apache.poi.ss.formula.BaseFormulaEvaluator;
  17. import org.apache.poi.ss.formula.EvaluationCell;
  18. import org.apache.poi.ss.formula.EvaluationWorkbook;
  19. import org.apache.poi.ss.formula.WorkbookEvaluator;
  20. import org.apache.poi.ss.formula.eval.BoolEval;
  21. import org.apache.poi.ss.formula.eval.ErrorEval;
  22. import org.apache.poi.ss.formula.eval.NumberEval;
  23. import org.apache.poi.ss.formula.eval.StringEval;
  24. import org.apache.poi.ss.formula.eval.ValueEval;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.CellType;
  27. import org.apache.poi.ss.usermodel.CellValue;
  28. import org.apache.poi.ss.usermodel.RichTextString;
  29. /**
  30. * Internal POI use only - parent of XSSF and SXSSF formula evaluators
  31. */
  32. public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
  33. protected BaseXSSFFormulaEvaluator(WorkbookEvaluator bookEvaluator) {
  34. super(bookEvaluator);
  35. }
  36. @Override
  37. protected RichTextString createRichTextString(String str) {
  38. return new XSSFRichTextString(str);
  39. }
  40. /**
  41. * Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
  42. */
  43. protected abstract EvaluationCell toEvaluationCell(Cell cell);
  44. /**
  45. * Returns a CellValue wrapper around the supplied ValueEval instance.
  46. */
  47. protected CellValue evaluateFormulaCellValue(Cell cell) {
  48. EvaluationCell evalCell = toEvaluationCell(cell);
  49. ValueEval eval = _bookEvaluator.evaluate(evalCell);
  50. if (eval instanceof NumberEval) {
  51. NumberEval ne = (NumberEval) eval;
  52. return new CellValue(ne.getNumberValue());
  53. }
  54. if (eval instanceof BoolEval) {
  55. BoolEval be = (BoolEval) eval;
  56. return CellValue.valueOf(be.getBooleanValue());
  57. }
  58. if (eval instanceof StringEval) {
  59. StringEval ne = (StringEval) eval;
  60. return new CellValue(ne.getStringValue());
  61. }
  62. if (eval instanceof ErrorEval) {
  63. return CellValue.getError(((ErrorEval)eval).getErrorCode());
  64. }
  65. throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
  66. }
  67. protected void setCellType(Cell cell, CellType cellType) {
  68. EvaluationWorkbook evaluationWorkbook = getEvaluationWorkbook();
  69. BaseXSSFEvaluationWorkbook xewb = BaseXSSFEvaluationWorkbook.class.isAssignableFrom(evaluationWorkbook.getClass()) ? (BaseXSSFEvaluationWorkbook) evaluationWorkbook : null;
  70. ((XSSFCell) cell).setCellType(cellType, xewb);
  71. }
  72. }