今天在博客网申请了博客,一直要动手开始写博客,直到今天才开始写博客。
写博客不是为了显摆什么,只是为了记录自己成长的一点历程,当回首时可以看到生活的一点点痕迹。
最近做了一个用POI设计excel2003的样式的东东,拿出来晒晒,欢迎看到的朋友多多指教。
废话不多说了,现在咱就进入正题。
---------------先做一点说明:文档中XXX表示可以根据需要自己选择的属性-------------------------
1.jar包介绍
poi-2.5.1-final-20040804.jar,请从google或百度上下载
2.几个重要类及方法的介绍
HSSFWorkbook:创建一个工作薄用到的类
常用方法:HSSFWorkbook()通过此构造方法创建一个工作薄对象wb
setSheetName(int sheet,String name,short encoding)sheet表示第几个sheet表,name表示sheet的名称,encoding表示sheet的name的编码格式
HSSFSheet:创建一个sheet
通过wb.crecreateSheet()创建一个sheet对象
setColumnWidth(short i,short j)设置列宽,i表示第几列,j表示该列的宽度,列的下标从0开始
addMergedRegion(Region region)合并region对象指定的单元格
HSSFCellStyle:设置sheet的样式
通过wb.createCellStyle()创建一个style对象
setFillPattern(HSSFCellStyle.XXX)设置填充样式
setFillForegroundColor(HSSFColor.XXX)设置填充色
注意:为单元格设置颜色时上面两句必须同时设置
以下四句是设置单元格上、下、左、右边框的样式
setBorderBottom(HSSFCellStyle.XXX)
setBorderLeft(HSSFCellStyle.XXX)
setBorderRight(HSSFCellStyle.XXX)
setBorderTop(HSSFCellStyle.XXX)
setWrapText(true)设置自动换行 true/false
setAlignment(HSSFCellStyle.XXX)设置单元格字体显示居中(左右方向)
setVerticalAlignment(HSSFCellStyle.XXX)设置单元格字体显示居中(上下方向)
HSSFRow:设置sheet行的类
通过sheet.createRow(int i)创建一行,其中i为行号,行号从0开始
createCell(short i)创建当前行所在的某列,列号从0开始
HSSFCell:设置sheet单元格的类,也可以说是列
setEncoding(HSSFCell.ENCODING_UTF_16) 设置单元格编码
setCellValue("输入你需要给单元格显示的值")设置单元格显示的值
setCellStyle(Style style)把设置好的style格式运用到指定的单元格上
Region:指定合并的单元格的区域
常用的构造方法Region(int a,short b,int c,short d)四个参数分别是起始行、起始列、结束行、结束列
HSSFFont:设置单元格字体
通过wb.createFont()创建一个字体对象
setBoldweight(short i)设置字体的宽度
setFontHeightInPoints(short i)设置字体的高度
setBoldweight(HSSFFont.XXX) 粗体显示
3.实现效果
4.源代码
1 package com.cn;
2
3 import java.io.FileOutputStream;
4 import org.apache.poi.hssf.usermodel.HSSFCell;
5 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
6 import org.apache.poi.hssf.usermodel.HSSFFont;
7 import org.apache.poi.hssf.usermodel.HSSFRow;
8 import org.apache.poi.hssf.usermodel.HSSFSheet;
9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
10 import org.apache.poi.hssf.util.HSSFColor;
11 import org.apache.poi.hssf.util.Region;
12
13 public class Excel2003Style {
14
15 public static void test2() throws Exception {
16 HSSFWorkbook wb = new HSSFWorkbook();// 创建一个工作薄
17 HSSFSheet sheet = wb.createSheet();// 创建一个sheet
18
19 /**
20 * 设置行高方法 row.setHeight((short) 390);此处的数字不是以像素为单位的,需要除以15.625才是像素数
21 * setHeightInPoints((short) 25);以像素为单位,此处的25即为25个像素
22 */
23 /**
24 * 设置列宽方法 setColumnWidth((short) a, (short)b)第一个参数表示要为第几列设置,第二个参数表示列的宽度
25 * 35.7*n n为行高的像素数
26 */
27 // 按需求设置相应列的宽度 列从0开始
28 sheet.setColumnWidth((short) 2, (short) 3200);
29 sheet.setColumnWidth((short) 3, (short) 3500);
30 sheet.setColumnWidth((short) 4, (short) 4000);
31 sheet.setColumnWidth((short) 5, (short) 4000);
32 sheet.setColumnWidth((short) 6, (short) 3200);
33 sheet.setColumnWidth((short) 8, (short) 4800);
34 sheet.setColumnWidth((short) 9, (short) 3600);
35
36 wb.setSheetName(0, "统计表", HSSFWorkbook.ENCODING_UTF_16); // 设置sheet0的名称和编码
37
38 // 设置style的样式,此样式运用在第一行
39 HSSFCellStyle style = wb.createCellStyle();
40 // 设置格式为居中
41 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
42 // 把设置的字体相关的属性应用到style上
43 HSSFFont font = wb.createFont();// 创建一个字体对象
44 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
45 style.setFont(font);
46
47 HSSFRow row = sheet.createRow(0);// 创建一行,此处创建的是第一行
48 // 创建一个cell对象(列) 格子单元
49 HSSFCell cell = row.createCell((short) 0);
50 // 设置单元格的编码
51 cell.setEncoding(HSSFCell.ENCODING_UTF_16);
52 cell.setCellValue("移动楼宇宽带通新装统计表");
53 // 把设置的格式运用到单元格上
54 cell.setCellStyle(style);
55
56 // 设置style1的样式,此样式运用在第二行
57 HSSFCellStyle style1 = wb.createCellStyle();// cell样式
58 // 设置单元格背景色,设置单元格背景色以下两句必须同时设置
59 style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 设置填充样式
60 style1.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 设置填充色
61 // 设置单元格上、下、左、右的边框线
62 style1.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
63 style1.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
64 style1.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
65 style1.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
66 HSSFFont font1 = wb.createFont();// 创建一个字体对象
67 font1.setBoldweight((short) 10);// 设置字体的宽度
68 font1.setFontHeightInPoints((short) 10);// 设置字体的高度
69 font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示
70 style1.setFont(font1);// 设置style1的字体
71 style1.setWrapText(true);// 设置自动换行
72 style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置单元格字体显示居中(左右方向)
73 style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置单元格字体显示居中(上下方向)
74
75 // 设置style2的样式,此样式运用在第一列、第二列从第四行开始的位置
76 HSSFCellStyle style2 = wb.createCellStyle();// cell样式
77 // 设置单元格上、下、左、右的边框线
78 style2.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
79 style2.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
80 style2.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
81 style2.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
82 style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置单元格字体居中(左右方向)
83 style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 设置字体显示居中(上下方向)
84
85 // 创建单元格、合并单元格、设置单元格样式
86 Region region = null;
87 // 合并单元格---Region()中构造函数四个参数分别代表起始行、起始列、结束行、结束列
88 region = new Region(0, (short) 0, 0, (short) 9);
89 sheet.addMergedRegion(region);
90 region = new Region(1, (short) 0, 2, (short) 1);
91 sheet.addMergedRegion(region);
92 for (short i = 2; i <= 10; i++) {
93 region = new Region(1, i, 2, i);
94 sheet.addMergedRegion(region);
95 sheet.createRow(1).createCell((short) (i - 2)).setCellStyle(style1);
96 sheet.createRow(2).createCell((short) (i - 2)).setCellStyle(style1);
97 }
98 sheet.createRow(1).createCell((short) 9).setCellStyle(style1);
99 sheet.createRow(2).createCell((short) 9).setCellStyle(style1);
100 sheet.createRow(1).createCell((short) 10).setCellStyle(style1);
101 sheet.createRow(2).createCell((short) 10).setCellStyle(style1);
102 // END 按要求合并第二行和第三行的单元格
103
104 HSSFCell cellb = null;
105 /**
106 * 设置第二行显示的10个单元格的文字 创建单元格、设置单元格字符编码、写值
107 * 背景色、字体、自动换行、边框样式(直接应用定义好的style1样式)
108 */
109 cellb = sheet.createRow(1).createCell((short) 0);
110 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
111 cellb.setCellValue("行政区域");
112 cellb.setCellStyle(style1);
113
114 cellb = sheet.createRow(1).createCell((short) 2);
115 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
116 cellb.setCellValue("PBOSS可受理楼宇数");
117 cellb.setCellStyle(style1);
118
119 cellb = sheet.createRow(1).createCell((short) 3);
120 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
121 cellb.setCellValue("上月用户数(户)");
122 cellb.setCellStyle(style1);
123
124 cellb = sheet.createRow(1).createCell((short) 4);
125 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
126 cellb.setCellValue("本月新装用户数(户)");
127 cellb.setCellStyle(style1);
128
129 cellb = sheet.createRow(1).createCell((short) 5);
130 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
131 cellb.setCellValue("本月拆机减少用户数(户)");
132 cellb.setCellStyle(style1);
133
134 cellb = sheet.createRow(1).createCell((short) 6);
135 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
136 cellb.setCellValue("本月合计用户数(户)");
137 cellb.setCellStyle(style1);
138
139 cellb = sheet.createRow(1).createCell((short) 7);
140 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
141 cellb.setCellValue("下月基数(户)");
142 cellb.setCellStyle(style1);
143
144 cellb = sheet.createRow(1).createCell((short) 8);
145 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
146 cellb.setCellValue("未按时完成的家庭业务安装工单数(户)");
147 cellb.setCellStyle(style1);
148
149 cellb = sheet.createRow(1).createCell((short) 9);
150 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
151 cellb.setCellValue("本月实际维护用户数(户)");
152 cellb.setCellStyle(style1);
153
154 cellb = sheet.createRow(1).createCell((short) 10);
155 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
156 cellb.setCellValue("平均安装工单时长(小时)");
157 cellb.setCellStyle(style1);
158
159 /**
160 * 第一、二列从第四行开始 合并单元格、设置单元格编码、写值 边框样式(直接应用定义好的style2样式)
161 */
162 region = new Region(3, (short) 0, 4, (short) 0);
163 sheet.addMergedRegion(region);
164 cellb = sheet.createRow(3).createCell((short) 0);
165 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
166 cellb.setCellValue("浦东");
167 cellb.setCellStyle(style2);
168 cellb = sheet.createRow(3).createCell((short) 1);
169 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
170 cellb.setCellValue("浦东新区");
171 cellb.setCellStyle(style2);
172 cellb = sheet.createRow(4).createCell((short) 1);
173 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
174 cellb.setCellValue("小计");
175 cellb.setCellStyle(style1);
176
177 region = new Region(5, (short) 0, 8, (short) 0);
178 sheet.addMergedRegion(region);
179 cellb = sheet.createRow(5).createCell((short) 0);
180 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
181 cellb.setCellValue("南区");
182 cellb.setCellStyle(style2);
183 cellb = sheet.createRow(5).createCell((short) 1);
184 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
185 cellb.setCellValue("黄浦区");
186 cellb.setCellStyle(style2);
187 cellb = sheet.createRow(6).createCell((short) 1);
188 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
189 cellb.setCellValue("卢湾区");
190 cellb.setCellStyle(style2);
191 cellb = sheet.createRow(7).createCell((short) 1);
192 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
193 cellb.setCellValue("徐汇区");
194 cellb.setCellStyle(style2);
195 cellb = sheet.createRow(8).createCell((short) 1);
196 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
197 cellb.setCellValue("小计");
198 cellb.setCellStyle(style1);
199
200 region = new Region(9, (short) 0, 12, (short) 0);
201 sheet.addMergedRegion(region);
202 cellb = sheet.createRow(9).createCell((short) 0);
203 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
204 cellb.setCellValue("西区");
205 cellb.setCellStyle(style2);
206 cellb = sheet.createRow(9).createCell((short) 1);
207 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
208 cellb.setCellValue("长宁区");
209 cellb.setCellStyle(style2);
210 cellb = sheet.createRow(10).createCell((short) 1);
211 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
212 cellb.setCellValue("普陀区");
213 cellb.setCellStyle(style2);
214 cellb = sheet.createRow(11).createCell((short) 1);
215 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
216 cellb.setCellValue("静安区");
217 cellb.setCellStyle(style2);
218 cellb = sheet.createRow(12).createCell((short) 1);
219 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
220 cellb.setCellValue("小计");
221 cellb.setCellStyle(style1);
222
223 region = new Region(13, (short) 0, 16, (short) 0);
224 sheet.addMergedRegion(region);
225 cellb = sheet.createRow(13).createCell((short) 0);
226 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
227 cellb.setCellValue("北区");
228 cellb.setCellStyle(style2);
229 cellb = sheet.createRow(13).createCell((short) 1);
230 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
231 cellb.setCellValue("闸北区");
232 cellb.setCellStyle(style2);
233 cellb = sheet.createRow(14).createCell((short) 1);
234 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
235 cellb.setCellValue("虹口区");
236 cellb.setCellStyle(style2);
237 cellb = sheet.createRow(15).createCell((short) 1);
238 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
239 cellb.setCellValue("杨浦区");
240 cellb.setCellStyle(style2);
241 cellb = sheet.createRow(16).createCell((short) 1);
242 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
243 cellb.setCellValue("小计");
244 cellb.setCellStyle(style1);
245
246 // 合并单元格 从18行到27行的第一列和第二列
247 for (int j = 17; j <= 26; j++) {
248 region = new Region(j, (short) 0, j, (short) 1);
249 sheet.addMergedRegion(region);
250 sheet.createRow(j).createCell((short) 1).setCellStyle(style2);
251 }
252 cellb = sheet.createRow(17).createCell((short) 0);
253 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
254 cellb.setCellValue("松江区");
255 cellb.setCellStyle(style2);
256 cellb = sheet.createRow(18).createCell((short) 0);
257 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
258 cellb.setCellValue("金山区");
259 cellb.setCellStyle(style2);
260 cellb = sheet.createRow(19).createCell((short) 0);
261 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
262 cellb.setCellValue("闵行区");
263 cellb.setCellStyle(style2);
264 cellb = sheet.createRow(20).createCell((short) 0);
265 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
266 cellb.setCellValue("奉贤区");
267 cellb.setCellStyle(style2);
268 cellb = sheet.createRow(21).createCell((short) 0);
269 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
270 cellb.setCellValue("嘉定区");
271 cellb.setCellStyle(style2);
272 cellb = sheet.createRow(22).createCell((short) 0);
273 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
274 cellb.setCellValue("青浦区");
275 cellb.setCellStyle(style2);
276 cellb = sheet.createRow(23).createCell((short) 0);
277 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
278 cellb.setCellValue("宝山区");
279 cellb.setCellStyle(style2);
280 cellb = sheet.createRow(24).createCell((short) 0);
281 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
282 cellb.setCellValue("崇明区");
283 cellb.setCellStyle(style2);
284 cellb = sheet.createRow(25).createCell((short) 0);
285 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
286 cellb.setCellValue("南汇区");
287 cellb.setCellStyle(style2);
288 cellb = sheet.createRow(26).createCell((short) 0);
289 cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
290 cellb.setCellValue("合计");
291 cellb.setCellStyle(style1);
292 // 设置空白区域的单元格的字符编码和边框样式
293 for (int a = 3; a <= 26; a++)
294 for (int b = 2; b <= 10; b++) {
295 sheet.createRow(a).createCell((short) b).setEncoding(
296 HSSFCell.ENCODING_UTF_16);
297 sheet.createRow(a).createCell((short) b).setCellStyle(style2);
298 }
299 // 设置第三行的行高,注意此句代码的位置
300 sheet.createRow(2).setHeightInPoints((short) 50);
301 // 定义工作薄的名称以及新建的路径
302 FileOutputStream output = new FileOutputStream(
303 "C:\\Users\\Administrator\\Desktop\\移动楼宇宽带通新装统计表(2003版本).xls");
304 wb.write(output);
305 output.close();
306 }
307
308 // 测试
309 public static void main(String[] args) throws Exception {
310 test2();
311 }
312 }