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.

XSSFCreationHelper.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.common.usermodel.HyperlinkType;
  17. import org.apache.poi.ss.usermodel.CreationHelper;
  18. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  19. import org.apache.poi.ss.usermodel.Hyperlink;
  20. import org.apache.poi.ss.usermodel.Workbook;
  21. import org.apache.poi.ss.util.AreaReference;
  22. import org.apache.poi.ss.util.CellReference;
  23. import org.apache.poi.util.Internal;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. public class XSSFCreationHelper implements CreationHelper {
  28. private final XSSFWorkbook workbook;
  29. private final Map<String, Workbook> referencedWorkbooks;
  30. /**
  31. * Should only be called by {@link XSSFWorkbook#getCreationHelper()}
  32. *
  33. * @param wb the workbook to create objects for
  34. */
  35. @Internal
  36. public XSSFCreationHelper(XSSFWorkbook wb) {
  37. workbook = wb;
  38. referencedWorkbooks = new HashMap<>();
  39. }
  40. /**
  41. * Creates a new XSSFRichTextString for you.
  42. */
  43. @Override
  44. public XSSFRichTextString createRichTextString(String text) {
  45. XSSFRichTextString rt = new XSSFRichTextString(text);
  46. rt.setStylesTableReference(workbook.getStylesSource());
  47. return rt;
  48. }
  49. @Override
  50. public XSSFDataFormat createDataFormat() {
  51. return workbook.createDataFormat();
  52. }
  53. @Override
  54. public XSSFColor createExtendedColor() {
  55. return XSSFColor.from(CTColor.Factory.newInstance(), workbook.getStylesSource().getIndexedColors());
  56. }
  57. /**
  58. * Create a new XSSFHyperlink.
  59. *
  60. * @param type - the type of hyperlink to create, see {@link Hyperlink}
  61. */
  62. @Override
  63. public XSSFHyperlink createHyperlink(HyperlinkType type) {
  64. return new XSSFHyperlink(type);
  65. }
  66. /**
  67. * Creates a XSSFFormulaEvaluator, the object that evaluates formula cells.
  68. *
  69. * @return a XSSFFormulaEvaluator instance
  70. */
  71. @Override
  72. public XSSFFormulaEvaluator createFormulaEvaluator() {
  73. XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(workbook);
  74. Map<String, FormulaEvaluator> evaluatorMap = new HashMap<>();
  75. evaluatorMap.put("", evaluator);
  76. this.referencedWorkbooks.forEach((name,otherWorkbook)->evaluatorMap.put(name,otherWorkbook.getCreationHelper().createFormulaEvaluator()));
  77. evaluator.setupReferencedWorkbooks(evaluatorMap);
  78. return evaluator;
  79. }
  80. /**
  81. * Creates a XSSFClientAnchor. Use this object to position drawing object in
  82. * a sheet
  83. *
  84. * @return a XSSFClientAnchor instance
  85. * @see org.apache.poi.ss.usermodel.Drawing
  86. */
  87. @Override
  88. public XSSFClientAnchor createClientAnchor() {
  89. return new XSSFClientAnchor();
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. @Override
  95. public AreaReference createAreaReference(String reference) {
  96. return new AreaReference(reference, workbook.getSpreadsheetVersion());
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. @Override
  102. public AreaReference createAreaReference(CellReference topLeft, CellReference bottomRight) {
  103. return new AreaReference(topLeft, bottomRight, workbook.getSpreadsheetVersion());
  104. }
  105. protected Map<String, Workbook> getReferencedWorkbooks() {
  106. return referencedWorkbooks;
  107. }
  108. protected void addExternalWorkbook(String name, Workbook workbook) {
  109. this.referencedWorkbooks.put(name,workbook);
  110. }
  111. }