KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > contrib > HSSFRegionUtil


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.usermodel.contrib;
20
21 import org.apache.poi.hssf.usermodel.HSSFCell;
22 import org.apache.poi.hssf.usermodel.HSSFRow;
23 import org.apache.poi.hssf.usermodel.HSSFSheet;
24 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25
26 import org.apache.poi.hssf.util.Region;
27
28 import org.apache.commons.lang.exception.NestableException;
29
30 /**
31  * Various utility functions that make working with a region of cells easier.
32  *
33  *@author Eric Pugh epugh@upstate.com
34  *@since July 29, 2002
35  */

36
37 public class HSSFRegionUtil
38 {
39
40   /** Constructor for the HSSFRegionUtil object */
41   private HSSFRegionUtil() { }
42
43   /**
44    * Sets the left border for a region of cells by manipulating the cell style
45    * of the indidual cells on the left
46    *
47    *@param border The new border
48    *@param region The region that should have the border
49    *@param workbook The workbook that the region is on.
50    *@param sheet The sheet that the region is on.
51    *@exception NestableException Thrown if the CellStyle can't be changed
52    */

53   public static void setBorderLeft( short border, Region region, HSSFSheet sheet, HSSFWorkbook workbook )
54     throws NestableException {
55     int rowStart = region.getRowFrom();
56     int rowEnd = region.getRowTo();
57     int column = region.getColumnFrom();
58
59     for ( int i = rowStart; i <= rowEnd; i++ ) {
60       HSSFRow row = HSSFCellUtil.getRow( i, sheet );
61       HSSFCell cell = HSSFCellUtil.getCell( row, column );
62       HSSFCellUtil.setCellStyleProperty( cell, workbook, "borderLeft", new Short JavaDoc( border ) );
63     }
64   }
65
66   /**
67    * Sets the leftBorderColor attribute of the HSSFRegionUtil object
68    *
69    *@param color The color of the border
70    *@param region The region that should have the border
71    *@param workbook The workbook that the region is on.
72    *@param sheet The sheet that the region is on.
73    *@exception NestableException Thrown if the CellStyle can't be changed
74    * properly.
75    */

76   public static void setLeftBorderColor( short color, Region region, HSSFSheet sheet, HSSFWorkbook workbook )
77     throws NestableException {
78     int rowStart = region.getRowFrom();
79     int rowEnd = region.getRowTo();
80     int column = region.getColumnFrom();
81
82     for ( int i = rowStart; i <= rowEnd; i++ ) {
83       HSSFRow row = HSSFCellUtil.getRow( i, sheet );
84       HSSFCell cell = HSSFCellUtil.getCell( row, column );
85       HSSFCellUtil.setCellStyleProperty( cell, workbook, "leftBorderColor", new Short JavaDoc( color ) );
86     }
87   }
88
89   /**
90    * Sets the borderRight attribute of the HSSFRegionUtil object
91    *
92    *@param border The new border
93    *@param region The region that should have the border
94    *@param workbook The workbook that the region is on.
95    *@param sheet The sheet that the region is on.
96    *@exception NestableException Thrown if the CellStyle can't be changed
97    */

98   public static void setBorderRight( short border, Region region, HSSFSheet sheet, HSSFWorkbook workbook )
99     throws NestableException {
100     int rowStart = region.getRowFrom();
101     int rowEnd = region.getRowTo();
102     int column = region.getColumnTo();
103
104     for ( int i = rowStart; i <= rowEnd; i++ ) {
105       HSSFRow row = HSSFCellUtil.getRow( i, sheet );
106       HSSFCell cell = HSSFCellUtil.getCell( row, column );
107
108       HSSFCellUtil.setCellStyleProperty( cell, workbook, "borderRight", new Short JavaDoc( border ) );
109     }
110   }
111
112   /**
113    * Sets the rightBorderColor attribute of the HSSFRegionUtil object
114    *
115    *@param color The color of the border
116    *@param region The region that should have the border
117    *@param workbook The workbook that the region is on.
118    *@param sheet The sheet that the region is on.
119    *@exception NestableException Thrown if the CellStyle can't be changed
120    * properly.
121    */

122   public static void setRightBorderColor( short color, Region region, HSSFSheet sheet, HSSFWorkbook workbook )
123     throws NestableException {
124     int rowStart = region.getRowFrom();
125     int rowEnd = region.getRowTo();
126     int column = region.getColumnTo();
127
128     for ( int i = rowStart; i <= rowEnd; i++ ) {
129       HSSFRow row = HSSFCellUtil.getRow( i, sheet );
130       HSSFCell cell = HSSFCellUtil.getCell( row, column );
131       HSSFCellUtil.setCellStyleProperty( cell, workbook, "rightBorderColor", new Short JavaDoc( color ) );
132     }
133   }
134
135   /**
136    * Sets the borderBottom attribute of the HSSFRegionUtil object
137    *
138    *@param border The new border
139    *@param region The region that should have the border
140    *@param workbook The workbook that the region is on.
141    *@param sheet The sheet that the region is on.
142    *@exception NestableException Thrown if the CellStyle can't be changed
143    */

144   public static void setBorderBottom( short border, Region region, HSSFSheet sheet, HSSFWorkbook workbook )
145     throws NestableException {
146     int colStart = region.getColumnFrom();
147     int colEnd = region.getColumnTo();
148     int rowIndex = region.getRowTo();
149     HSSFRow row = HSSFCellUtil.getRow( rowIndex, sheet );
150     for ( int i = colStart; i <= colEnd; i++ ) {
151
152       HSSFCell cell = HSSFCellUtil.getCell( row, i );
153       HSSFCellUtil.setCellStyleProperty( cell, workbook, "borderBottom", new Short JavaDoc( border ) );
154     }
155   }
156
157   /**
158    * Sets the bottomBorderColor attribute of the HSSFRegionUtil object
159    *
160    *@param color The color of the border
161    *@param region The region that should have the border
162    *@param workbook The workbook that the region is on.
163    *@param sheet The sheet that the region is on.
164    *@exception NestableException Thrown if the CellStyle can't be changed
165    * properly.
166    */

167   public static void setBottomBorderColor( short color, Region region, HSSFSheet sheet, HSSFWorkbook workbook )
168     throws NestableException {
169     int colStart = region.getColumnFrom();
170     int colEnd = region.getColumnTo();
171     int rowIndex = region.getRowTo();
172     HSSFRow row = HSSFCellUtil.getRow( rowIndex, sheet );
173     for ( int i = colStart; i <= colEnd; i++ ) {
174       HSSFCell cell = HSSFCellUtil.getCell( row, i );
175       HSSFCellUtil.setCellStyleProperty( cell, workbook, "bottomBorderColor", new Short JavaDoc( color ) );
176     }
177   }
178
179
180   /**
181    * Sets the borderBottom attribute of the HSSFRegionUtil object
182    *
183    *@param border The new border
184    *@param region The region that should have the border
185    *@param workbook The workbook that the region is on.
186    *@param sheet The sheet that the region is on.
187    *@exception NestableException Thrown if the CellStyle can't be changed
188    */

189   public static void setBorderTop( short border, Region region, HSSFSheet sheet, HSSFWorkbook workbook )
190     throws NestableException {
191     int colStart = region.getColumnFrom();
192     int colEnd = region.getColumnTo();
193     int rowIndex = region.getRowFrom();
194     HSSFRow row = HSSFCellUtil.getRow( rowIndex, sheet );
195     for ( int i = colStart; i <= colEnd; i++ ) {
196
197       HSSFCell cell = HSSFCellUtil.getCell( row, i );
198       HSSFCellUtil.setCellStyleProperty( cell, workbook, "borderTop", new Short JavaDoc( border ) );
199     }
200   }
201
202   /**
203    * Sets the topBorderColor attribute of the HSSFRegionUtil object
204    *
205    *@param color The color of the border
206    *@param region The region that should have the border
207    *@param workbook The workbook that the region is on.
208    *@param sheet The sheet that the region is on.
209    *@exception NestableException Thrown if the CellStyle can't be changed
210    * properly.
211    */

212   public static void setTopBorderColor( short color, Region region, HSSFSheet sheet, HSSFWorkbook workbook )
213     throws NestableException {
214     int colStart = region.getColumnFrom();
215     int colEnd = region.getColumnTo();
216     int rowIndex = region.getRowFrom();
217     HSSFRow row = HSSFCellUtil.getRow( rowIndex, sheet );
218     for ( int i = colStart; i <= colEnd; i++ ) {
219       HSSFCell cell = HSSFCellUtil.getCell( row, i );
220       HSSFCellUtil.setCellStyleProperty( cell, workbook, "topBorderColor", new Short JavaDoc( color ) );
221
222     }
223   }
224
225 }
226
227
Popular Tags