001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel.contrib;
019:
020: import org.apache.poi.hssf.usermodel.HSSFCell;
021: import org.apache.poi.hssf.usermodel.HSSFRow;
022: import org.apache.poi.hssf.usermodel.HSSFSheet;
023: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
024:
025: import org.apache.poi.hssf.util.Region;
026:
027: import org.apache.commons.lang.exception.NestableException;
028:
029: /**
030: * Various utility functions that make working with a region of cells easier.
031: *
032: *@author Eric Pugh epugh@upstate.com
033: *@since July 29, 2002
034: */
035:
036: public class HSSFRegionUtil {
037:
038: /** Constructor for the HSSFRegionUtil object */
039: private HSSFRegionUtil() {
040: }
041:
042: /**
043: * Sets the left border for a region of cells by manipulating the cell style
044: * of the indidual cells on the left
045: *
046: *@param border The new border
047: *@param region The region that should have the border
048: *@param workbook The workbook that the region is on.
049: *@param sheet The sheet that the region is on.
050: *@exception NestableException Thrown if the CellStyle can't be changed
051: */
052: public static void setBorderLeft(short border, Region region,
053: HSSFSheet sheet, HSSFWorkbook workbook)
054: throws NestableException {
055: int rowStart = region.getRowFrom();
056: int rowEnd = region.getRowTo();
057: int column = region.getColumnFrom();
058:
059: for (int i = rowStart; i <= rowEnd; i++) {
060: HSSFRow row = HSSFCellUtil.getRow(i, sheet);
061: HSSFCell cell = HSSFCellUtil.getCell(row, column);
062: HSSFCellUtil.setCellStyleProperty(cell, workbook,
063: "borderLeft", new Short(border));
064: }
065: }
066:
067: /**
068: * Sets the leftBorderColor attribute of the HSSFRegionUtil object
069: *
070: *@param color The color of the border
071: *@param region The region that should have the border
072: *@param workbook The workbook that the region is on.
073: *@param sheet The sheet that the region is on.
074: *@exception NestableException Thrown if the CellStyle can't be changed
075: * properly.
076: */
077: public static void setLeftBorderColor(short color, Region region,
078: HSSFSheet sheet, HSSFWorkbook workbook)
079: throws NestableException {
080: int rowStart = region.getRowFrom();
081: int rowEnd = region.getRowTo();
082: int column = region.getColumnFrom();
083:
084: for (int i = rowStart; i <= rowEnd; i++) {
085: HSSFRow row = HSSFCellUtil.getRow(i, sheet);
086: HSSFCell cell = HSSFCellUtil.getCell(row, column);
087: HSSFCellUtil.setCellStyleProperty(cell, workbook,
088: "leftBorderColor", new Short(color));
089: }
090: }
091:
092: /**
093: * Sets the borderRight attribute of the HSSFRegionUtil object
094: *
095: *@param border The new border
096: *@param region The region that should have the border
097: *@param workbook The workbook that the region is on.
098: *@param sheet The sheet that the region is on.
099: *@exception NestableException Thrown if the CellStyle can't be changed
100: */
101: public static void setBorderRight(short border, Region region,
102: HSSFSheet sheet, HSSFWorkbook workbook)
103: throws NestableException {
104: int rowStart = region.getRowFrom();
105: int rowEnd = region.getRowTo();
106: int column = region.getColumnTo();
107:
108: for (int i = rowStart; i <= rowEnd; i++) {
109: HSSFRow row = HSSFCellUtil.getRow(i, sheet);
110: HSSFCell cell = HSSFCellUtil.getCell(row, column);
111:
112: HSSFCellUtil.setCellStyleProperty(cell, workbook,
113: "borderRight", new Short(border));
114: }
115: }
116:
117: /**
118: * Sets the rightBorderColor attribute of the HSSFRegionUtil object
119: *
120: *@param color The color of the border
121: *@param region The region that should have the border
122: *@param workbook The workbook that the region is on.
123: *@param sheet The sheet that the region is on.
124: *@exception NestableException Thrown if the CellStyle can't be changed
125: * properly.
126: */
127: public static void setRightBorderColor(short color, Region region,
128: HSSFSheet sheet, HSSFWorkbook workbook)
129: throws NestableException {
130: int rowStart = region.getRowFrom();
131: int rowEnd = region.getRowTo();
132: int column = region.getColumnTo();
133:
134: for (int i = rowStart; i <= rowEnd; i++) {
135: HSSFRow row = HSSFCellUtil.getRow(i, sheet);
136: HSSFCell cell = HSSFCellUtil.getCell(row, column);
137: HSSFCellUtil.setCellStyleProperty(cell, workbook,
138: "rightBorderColor", new Short(color));
139: }
140: }
141:
142: /**
143: * Sets the borderBottom attribute of the HSSFRegionUtil object
144: *
145: *@param border The new border
146: *@param region The region that should have the border
147: *@param workbook The workbook that the region is on.
148: *@param sheet The sheet that the region is on.
149: *@exception NestableException Thrown if the CellStyle can't be changed
150: */
151: public static void setBorderBottom(short border, Region region,
152: HSSFSheet sheet, HSSFWorkbook workbook)
153: throws NestableException {
154: int colStart = region.getColumnFrom();
155: int colEnd = region.getColumnTo();
156: int rowIndex = region.getRowTo();
157: HSSFRow row = HSSFCellUtil.getRow(rowIndex, sheet);
158: for (int i = colStart; i <= colEnd; i++) {
159:
160: HSSFCell cell = HSSFCellUtil.getCell(row, i);
161: HSSFCellUtil.setCellStyleProperty(cell, workbook,
162: "borderBottom", new Short(border));
163: }
164: }
165:
166: /**
167: * Sets the bottomBorderColor attribute of the HSSFRegionUtil object
168: *
169: *@param color The color of the border
170: *@param region The region that should have the border
171: *@param workbook The workbook that the region is on.
172: *@param sheet The sheet that the region is on.
173: *@exception NestableException Thrown if the CellStyle can't be changed
174: * properly.
175: */
176: public static void setBottomBorderColor(short color, Region region,
177: HSSFSheet sheet, HSSFWorkbook workbook)
178: throws NestableException {
179: int colStart = region.getColumnFrom();
180: int colEnd = region.getColumnTo();
181: int rowIndex = region.getRowTo();
182: HSSFRow row = HSSFCellUtil.getRow(rowIndex, sheet);
183: for (int i = colStart; i <= colEnd; i++) {
184: HSSFCell cell = HSSFCellUtil.getCell(row, i);
185: HSSFCellUtil.setCellStyleProperty(cell, workbook,
186: "bottomBorderColor", new Short(color));
187: }
188: }
189:
190: /**
191: * Sets the borderBottom attribute of the HSSFRegionUtil object
192: *
193: *@param border The new border
194: *@param region The region that should have the border
195: *@param workbook The workbook that the region is on.
196: *@param sheet The sheet that the region is on.
197: *@exception NestableException Thrown if the CellStyle can't be changed
198: */
199: public static void setBorderTop(short border, Region region,
200: HSSFSheet sheet, HSSFWorkbook workbook)
201: throws NestableException {
202: int colStart = region.getColumnFrom();
203: int colEnd = region.getColumnTo();
204: int rowIndex = region.getRowFrom();
205: HSSFRow row = HSSFCellUtil.getRow(rowIndex, sheet);
206: for (int i = colStart; i <= colEnd; i++) {
207:
208: HSSFCell cell = HSSFCellUtil.getCell(row, i);
209: HSSFCellUtil.setCellStyleProperty(cell, workbook,
210: "borderTop", new Short(border));
211: }
212: }
213:
214: /**
215: * Sets the topBorderColor attribute of the HSSFRegionUtil object
216: *
217: *@param color The color of the border
218: *@param region The region that should have the border
219: *@param workbook The workbook that the region is on.
220: *@param sheet The sheet that the region is on.
221: *@exception NestableException Thrown if the CellStyle can't be changed
222: * properly.
223: */
224: public static void setTopBorderColor(short color, Region region,
225: HSSFSheet sheet, HSSFWorkbook workbook)
226: throws NestableException {
227: int colStart = region.getColumnFrom();
228: int colEnd = region.getColumnTo();
229: int rowIndex = region.getRowFrom();
230: HSSFRow row = HSSFCellUtil.getRow(rowIndex, sheet);
231: for (int i = colStart; i <= colEnd; i++) {
232: HSSFCell cell = HSSFCellUtil.getCell(row, i);
233: HSSFCellUtil.setCellStyleProperty(cell, workbook,
234: "topBorderColor", new Short(color));
235:
236: }
237: }
238:
239: }
|