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.

RegionUtil.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 org.apache.poi.ss.usermodel.BorderStyle;
  17. import org.apache.poi.ss.usermodel.Cell;
  18. import org.apache.poi.ss.usermodel.Row;
  19. import org.apache.poi.ss.usermodel.Sheet;
  20. /**
  21. * Various utility functions that make working with a region of cells easier.
  22. */
  23. public final class RegionUtil {
  24. private RegionUtil() {
  25. // no instances of this class
  26. }
  27. /**
  28. * For setting the same property on many cells to the same value
  29. */
  30. private static final class CellPropertySetter {
  31. private final String _propertyName;
  32. private final Object _propertyValue;
  33. public CellPropertySetter(String propertyName, int value) {
  34. _propertyName = propertyName;
  35. _propertyValue = Integer.valueOf(value);
  36. }
  37. public CellPropertySetter(String propertyName, BorderStyle value) {
  38. _propertyName = propertyName;
  39. _propertyValue = value;
  40. }
  41. public void setProperty(Row row, int column) {
  42. // create cell if it does not exist
  43. Cell cell = CellUtil.getCell(row, column);
  44. CellUtil.setCellStyleProperty(cell, _propertyName, _propertyValue);
  45. }
  46. }
  47. /**
  48. * Sets the left border style for a region of cells by manipulating the cell style of the individual
  49. * cells on the left
  50. *
  51. * @param border The new border
  52. * @param region The region that should have the border
  53. * @param sheet The sheet that the region is on.
  54. * @since POI 3.16 beta 1
  55. */
  56. public static void setBorderLeft(BorderStyle border, CellRangeAddress region, Sheet sheet) {
  57. int rowStart = region.getFirstRow();
  58. int rowEnd = region.getLastRow();
  59. int column = region.getFirstColumn();
  60. CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_LEFT, border);
  61. for (int i = rowStart; i <= rowEnd; i++) {
  62. cps.setProperty(CellUtil.getRow(i, sheet), column);
  63. }
  64. }
  65. /**
  66. * Sets the left border color for a region of cells by manipulating the cell style of the individual
  67. * cells on the left
  68. *
  69. * @param color The color of the border
  70. * @param region The region that should have the border
  71. * @param sheet The sheet that the region is on.
  72. * @since POI 3.15 beta 2
  73. */
  74. public static void setLeftBorderColor(int color, CellRangeAddress region, Sheet sheet) {
  75. int rowStart = region.getFirstRow();
  76. int rowEnd = region.getLastRow();
  77. int column = region.getFirstColumn();
  78. CellPropertySetter cps = new CellPropertySetter(CellUtil.LEFT_BORDER_COLOR, color);
  79. for (int i = rowStart; i <= rowEnd; i++) {
  80. cps.setProperty(CellUtil.getRow(i, sheet), column);
  81. }
  82. }
  83. /**
  84. * Sets the right border style for a region of cells by manipulating the cell style of the individual
  85. * cells on the right
  86. *
  87. * @param border The new border
  88. * @param region The region that should have the border
  89. * @param sheet The sheet that the region is on.
  90. * @since POI 3.16 beta 1
  91. */
  92. public static void setBorderRight(BorderStyle border, CellRangeAddress region, Sheet sheet) {
  93. int rowStart = region.getFirstRow();
  94. int rowEnd = region.getLastRow();
  95. int column = region.getLastColumn();
  96. CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_RIGHT, border);
  97. for (int i = rowStart; i <= rowEnd; i++) {
  98. cps.setProperty(CellUtil.getRow(i, sheet), column);
  99. }
  100. }
  101. /**
  102. * Sets the right border color for a region of cells by manipulating the cell style of the individual
  103. * cells on the right
  104. *
  105. * @param color The color of the border
  106. * @param region The region that should have the border
  107. * @param sheet The sheet that the region is on.
  108. * @since POI 3.15 beta 2
  109. */
  110. public static void setRightBorderColor(int color, CellRangeAddress region, Sheet sheet) {
  111. int rowStart = region.getFirstRow();
  112. int rowEnd = region.getLastRow();
  113. int column = region.getLastColumn();
  114. CellPropertySetter cps = new CellPropertySetter(CellUtil.RIGHT_BORDER_COLOR, color);
  115. for (int i = rowStart; i <= rowEnd; i++) {
  116. cps.setProperty(CellUtil.getRow(i, sheet), column);
  117. }
  118. }
  119. /**
  120. * Sets the bottom border style for a region of cells by manipulating the cell style of the individual
  121. * cells on the bottom
  122. *
  123. * @param border The new border
  124. * @param region The region that should have the border
  125. * @param sheet The sheet that the region is on.
  126. * @since POI 3.16 beta 1
  127. */
  128. public static void setBorderBottom(BorderStyle border, CellRangeAddress region, Sheet sheet) {
  129. int colStart = region.getFirstColumn();
  130. int colEnd = region.getLastColumn();
  131. int rowIndex = region.getLastRow();
  132. CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_BOTTOM, border);
  133. Row row = CellUtil.getRow(rowIndex, sheet);
  134. for (int i = colStart; i <= colEnd; i++) {
  135. cps.setProperty(row, i);
  136. }
  137. }
  138. /**
  139. * Sets the bottom border color for a region of cells by manipulating the cell style of the individual
  140. * cells on the bottom
  141. *
  142. * @param color The color of the border
  143. * @param region The region that should have the border
  144. * @param sheet The sheet that the region is on.
  145. * @since POI 3.15 beta 2
  146. */
  147. public static void setBottomBorderColor(int color, CellRangeAddress region, Sheet sheet) {
  148. int colStart = region.getFirstColumn();
  149. int colEnd = region.getLastColumn();
  150. int rowIndex = region.getLastRow();
  151. CellPropertySetter cps = new CellPropertySetter(CellUtil.BOTTOM_BORDER_COLOR, color);
  152. Row row = CellUtil.getRow(rowIndex, sheet);
  153. for (int i = colStart; i <= colEnd; i++) {
  154. cps.setProperty(row, i);
  155. }
  156. }
  157. /**
  158. * Sets the top border style for a region of cells by manipulating the cell style of the individual
  159. * cells on the top
  160. *
  161. * @param border The new border
  162. * @param region The region that should have the border
  163. * @param sheet The sheet that the region is on.
  164. * @since POI 3.16 beta 1
  165. */
  166. public static void setBorderTop(BorderStyle border, CellRangeAddress region, Sheet sheet) {
  167. int colStart = region.getFirstColumn();
  168. int colEnd = region.getLastColumn();
  169. int rowIndex = region.getFirstRow();
  170. CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_TOP, border);
  171. Row row = CellUtil.getRow(rowIndex, sheet);
  172. for (int i = colStart; i <= colEnd; i++) {
  173. cps.setProperty(row, i);
  174. }
  175. }
  176. /**
  177. * Sets the top border color for a region of cells by manipulating the cell style of the individual
  178. * cells on the top
  179. *
  180. * @param color The color of the border
  181. * @param region The region that should have the border
  182. * @param sheet The sheet that the region is on.
  183. * @since POI 3.15 beta 2
  184. */
  185. public static void setTopBorderColor(int color, CellRangeAddress region, Sheet sheet) {
  186. int colStart = region.getFirstColumn();
  187. int colEnd = region.getLastColumn();
  188. int rowIndex = region.getFirstRow();
  189. CellPropertySetter cps = new CellPropertySetter(CellUtil.TOP_BORDER_COLOR, color);
  190. Row row = CellUtil.getRow(rowIndex, sheet);
  191. for (int i = colStart; i <= colEnd; i++) {
  192. cps.setProperty(row, i);
  193. }
  194. }
  195. }