java导出excel代码Word格式文档下载.docx
《java导出excel代码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《java导出excel代码Word格式文档下载.docx(29页珍藏版)》请在冰豆网上搜索。
cache-control"
13.
expires"
0"
14.
keywords"
keyword1,keyword2,keyword3"
15.
description"
This
is
my
page"
16.
/head>
17.
18.
body>
19.
导出excel
20.
form
action="
excel"
method="
post"
21.
input
type="
submit"
name="
value="
导出"
22.
/form>
23.
/body>
24.<
/html>
Xml代码
servlet>
2.
servlet-name>
outPutExcel<
/servlet-name>
3.
servlet-class>
output.OutputExcel<
/servlet-class>
4.<
/servlet>
5.<
servlet-mapping>
7.
url-pattern>
/excel<
/url-pattern>
/servlet-mapping>
Java代码
1.package
output;
3.import
java.io.File;
4.import
java.io.FileNotFoundException;
5.import
java.io.FileOutputStream;
6.import
java.io.IOException;
8.import
org.apache.poi.hssf.usermodel.HSSFCell;
9.import
org.apache.poi.hssf.usermodel.HSSFCellStyle;
10.import
org.apache.poi.hssf.usermodel.HSSFFont;
11.import
org.apache.poi.hssf.usermodel.HSSFRichTextString;
12.import
org.apache.poi.hssf.usermodel.HSSFRow;
13.import
org.apache.poi.hssf.usermodel.HSSFSheet;
14.import
org.apache.poi.hssf.usermodel.HSSFWorkbook;
15.import
org.apache.poi.hssf.util.HSSFColor;
16.import
org.apache.poi.hssf.util.Region;
18./**
19.*
EXCEL报表工具类.
20.*
21.*
@author
sun
22.*
@version
23.*/
24.public
class
ExportExcel
{
25.
26.
private
HSSFWorkbook
wb
null;
27.
HSSFSheet
sheet
28.
29.
/**
30.
*
@param
31.
32.
*/
33.
public
ExportExcel(HSSFWorkbook
wb,
sheet)
34.
//super();
35.
this.wb
wb;
36.
this.sheet
sheet;
37.
}
38.
39.
40.
创建通用EXCEL头部
41.
42.
headString
头部显示的字符
43.
colSum
该报表的列数
44.
45.
void
createNormalHead(String
headString,
int
colSum)
46.
47.
HSSFRow
row
sheet.createRow(0);
48.
49.
//
设置第一行
50.
HSSFCell
cell
row.createCell(0);
51.
//row.setHeight((short)
1000);
52.
53.
定义单元格为字符串类型
54.
cell.setCellType(HSSFCell.ENCODING_UTF_16);
中文处理
55.
cell.setCellValue(new
HSSFRichTextString(headString));
56.
57.
指定合并区域
58.
59.
Region(int
rowFrom,
60.
short
colFrom,
61.
rowTo,
62.
colTo)
63.
64.
sheet.addMergedRegion(new
Region(0,
(short)
0,
colSum));
65.
66.
//定义单元格格式,添加单元格表样式,并添加到工作簿
67.
HSSFCellStyle
cellStyle
wb.createCellStyle();
68.
//设置单元格水平对齐类型
69.
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
指定单元格居中对齐
70.
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
指定单元格垂直居中对齐
71.
cellStyle.setWrapText(true);
指定单元格自动换行
72.
73.
设置单元格字体
74.
HSSFFont
font
wb.createFont();
75.
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
76.
//font.setFontName("
宋体"
);
77.
//font.setFontHeight((short)
600);
78.
//cellStyle.setFont(font);
79.
cell.setCellStyle(cellStyle);
80.
81.
82.
83.
创建通用报表第二行
84.
85.
params
统计条件数组
86.
需要合并到的列索引
87.
88.
createNormalTwoRow(String[]
params,
89.
//创建第二行
90.
row1
sheet.createRow
(1);
91.
92.
row1.setHeight((short)
400);
93.
94.
cell2
row1.createCell(0);
95.
96.
cell2.setCellType(HSSFCell.ENCODING_UTF_16);
97.
cell2.setCellValue(new
HSSFRichTextString("
时间:
+
params[0]
至"
params[1]));
98.
99.
100.
101.
102.
103.
104.
105.
106.
Region(1,
1,
107.
108.
109.
110.
111.
112.
113.
114.
115.
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
116.
font.setFontName("
117.
font.setFontHeight((short)
250);
118.
cellStyle.setFont(font);
119.
120.
cell2.setCellStyle(cellStyle);
121.
122.
123.
设置报表标题
124.
125.
columHeader
标题字符串数组
126.
127.
createColumHeader(String[]
columHeader)
128.
129.
设置列头
在第三行
130.
row2
sheet.createRow
(2);
131.
132.
指定行高
133.
row2.setHeight((short)
134.
135.
136.
137.
138.
139.
140.
单元格字体
141.
142.
143.
144.
145.
146.
147.
/*cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
设置单无格的边框为粗体
148.
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
设置单元格的边框颜色.
149.
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
150.
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
151.
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
152.
cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
153.
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
154.
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
155.
156.
设置单元格背景色
157.
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
158.
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
159.
160.
cell3
161.
162.
for
(int
i
0;
columHeader.length;
i++)
163.
row2.createCell(i);
164.
cell3.setCellType(HSSFCell.ENCODING_UTF_16);
165.
cell3.setCellStyle(cellStyle);
166.
cell3.setCellValue(new
HSSFRichTextString(columHeader[i]));
167.
168.
169.
170.
171.
创建内容单元格
172.
173.
174.
175.
col
short型的列索引
176.
align
对齐方式
177.
val
列值
178.
179.
cteateCell(HSSFWorkbook
row,
col,short
align,
String
val)
180.
row.createCell(col);
181.
182.
HSSFRichTextString(val));
183.
cellstyle
184.
cellstyle.setAlignment(align);
185.
cell.setCellStyle(cellstyle);
186.
187.
188.
创建合计行
189.
190.
cellValue
191.
192.
createLastSumRow(int
colSum,
String[]
cellValue)
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
//获取工作表最后一行
206.
lastRow
sheet.createRow((short)
(sheet.getLastRowNum()
1));
207.
sumCell
lastRow.createCell(0);
208.
209.
sumCell.setCellValue(new
合计"
));
210.
sumCell.setCellStyle(cellStyle);
211.
//合并
最后一行的第零列-最后一行的第一列
212.
Region(sheet.getLastRowNum(),
0,sheet.getLastRowNum(),
213.
214.
2;
(cellValue.length
2);
215.
//定义最后一行的第三列
216.
lastRow.createCell(i);
217.
218.
//定义数组
从0开始。
219.
HSSFRichTextString(cellValue[i-2]));
220.
221.
222.
223.
输入EXCEL文件
224.
225.