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.

SheetBuilder.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.ss.util;
  16. import java.util.Calendar;
  17. import java.util.Date;
  18. import org.apache.poi.ss.usermodel.Sheet;
  19. import org.apache.poi.ss.usermodel.Workbook;
  20. import org.apache.poi.ss.usermodel.Row;
  21. import org.apache.poi.ss.usermodel.Cell;
  22. /**
  23. * Class {@code SheetBuilder} provides an easy way of building workbook sheets
  24. * from 2D array of Objects. It can be used in test cases to improve code
  25. * readability or in Swing applications with tables.
  26. *
  27. * @author Roman Kashitsyn
  28. */
  29. public class SheetBuilder {
  30. private final Workbook workbook;
  31. private final Object[][] cells;
  32. private boolean shouldCreateEmptyCells;
  33. private String sheetName;
  34. public SheetBuilder(Workbook workbook, Object[][] cells) {
  35. this.workbook = workbook;
  36. this.cells = cells.clone();
  37. }
  38. /**
  39. * Returns {@code true} if null array elements should be treated as empty
  40. * cells.
  41. *
  42. * @return {@code true} if null objects should be treated as empty cells
  43. * and {@code false} otherwise
  44. */
  45. public boolean getCreateEmptyCells() {
  46. return shouldCreateEmptyCells;
  47. }
  48. /**
  49. * Specifies if null array elements should be treated as empty cells.
  50. *
  51. * @param shouldCreateEmptyCells {@code true} if null array elements should be
  52. * treated as empty cells
  53. * @return {@code this}
  54. */
  55. public SheetBuilder setCreateEmptyCells(boolean shouldCreateEmptyCells) {
  56. this.shouldCreateEmptyCells = shouldCreateEmptyCells;
  57. return this;
  58. }
  59. /**
  60. * Specifies name of the sheet to build. If not specified, default name (provided by
  61. * workbook) will be used instead.
  62. * @param sheetName sheet name to use
  63. * @return {@code this}
  64. */
  65. public SheetBuilder setSheetName(String sheetName) {
  66. this.sheetName = sheetName;
  67. return this;
  68. }
  69. /**
  70. * Builds sheet from parent workbook and 2D array with cell
  71. * values. Creates rows anyway (even if row contains only null
  72. * cells), creates cells if either corresponding array value is not
  73. * null or createEmptyCells property is true.
  74. * The conversion is performed in the following way:
  75. * <p>
  76. * <ul>
  77. * <li>Numbers become numeric cells.</li>
  78. * <li><code>java.util.Date</code> or <code>java.util.Calendar</code>
  79. * instances become date cells.</li>
  80. * <li>String with leading '=' char become formulas (leading '='
  81. * will be truncated).</li>
  82. * <li>Other objects become strings via <code>Object.toString()</code>
  83. * method call.</li>
  84. * </ul>
  85. *
  86. * @return newly created sheet
  87. */
  88. public Sheet build() {
  89. Sheet sheet = (sheetName == null) ? workbook.createSheet() : workbook.createSheet(sheetName);
  90. Row currentRow;
  91. Cell currentCell;
  92. for (int rowIndex = 0; rowIndex < cells.length; ++rowIndex) {
  93. Object[] rowArray = cells[rowIndex];
  94. currentRow = sheet.createRow(rowIndex);
  95. for (int cellIndex = 0; cellIndex < rowArray.length; ++cellIndex) {
  96. Object cellValue = rowArray[cellIndex];
  97. if (cellValue != null || shouldCreateEmptyCells) {
  98. currentCell = currentRow.createCell(cellIndex);
  99. setCellValue(currentCell, cellValue);
  100. }
  101. }
  102. }
  103. return sheet;
  104. }
  105. /**
  106. * Sets the cell value using object type information.
  107. *
  108. * @param cell cell to change
  109. * @param value value to set
  110. */
  111. private void setCellValue(Cell cell, Object value) {
  112. if (value == null || cell == null) {
  113. return;
  114. }
  115. if (value instanceof Number) {
  116. double doubleValue = ((Number) value).doubleValue();
  117. cell.setCellValue(doubleValue);
  118. } else if (value instanceof Date) {
  119. cell.setCellValue((Date) value);
  120. } else if (value instanceof Calendar) {
  121. cell.setCellValue((Calendar) value);
  122. } else if (isFormulaDefinition(value)) {
  123. cell.setCellFormula(getFormula(value));
  124. } else {
  125. cell.setCellValue(value.toString());
  126. }
  127. }
  128. private boolean isFormulaDefinition(Object obj) {
  129. if (obj instanceof String) {
  130. String str = (String) obj;
  131. return str.length() >= 2 && str.charAt(0) == '=';
  132. } else {
  133. return false;
  134. }
  135. }
  136. private String getFormula(Object obj) {
  137. return ((String) obj).substring(1);
  138. }
  139. }