a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 399|回复: 0

[专业语言] Java认证辅导:用java生成excel功能实现

[复制链接]
发表于 2012-8-4 12:44:44 | 显示全部楼层 |阅读模式
Java认证辅导:用java生成excel功能实现1 E6 K, p7 J/ U. ~0 |
使用java生成excel功能实现
- A0 @7 x+ O; |. f" M) m9 z1.jsp页面* U9 ]" l6 n/ S6 K- b% y
发送请求地址:s9p67_createExcel.do4 P* V  F8 _& J. r: [4 P7 C; u2 ]5 G; l% `
public String createExcel() {5 I8 {4 g( A& c; S
try {1 ~% C4 X9 d6 t8 b+ i: c) [
HttpServletResponse response = ServletActionContext.getResponse();
2 c+ x, E+ ?* SOutputStream os = response.getOutputStream();* M5 v& F. r$ m  e* m
response.reset();6 A& V1 y/ W3 d9 M* Z: X, o
response.setContentType(“application/msexcel;charset=gbk”); //设置响应格式# O; }" v: d1 a) J! C
//设置响应头,即报表头
: F1 ]: V% p7 bresponse.setHeader(“Content-disposition”, “attachment;filename=” + new String(“业务受理量统计报表”.getBytes(), “iso-8859-1”) + “.xls”);- n# ^5 {  e7 m1 {
HSSFWorkbook hwb = new HSSFWorkbook();; m) }, t/ p' G: X0 k! i3 B
HSSFSheet sheet = hwb.createSheet(“new Sheet”);
' \) Y9 Z# A2 U0 Lhwb.setSheetName(0, “业务受理量统计”);
, F3 v& u2 ^+ L# t  ?# R/ z+ rHSSFRow row = sheet.createRow((short) 0);  H2 G- r0 @/ X9 }& D+ o8 s
sheet.createFreezePane(0, 1);//设置第一行显示内容, Q" Q* A, a! h4 |. z: C
createCell(hwb, row, (short) 0, “业务类型”);+ M& }6 z7 D& d
createCell(hwb, row, (short) 1, “操作名称”);1 _+ W2 ?0 b- W+ `$ j
createCell(hwb, row, (short) 2, “数量”);
6 ]" S3 g5 S  n& j; h* b" G& hcreateCell(hwb, row, (short) 3, “收取金额”);
8 ~5 v0 |: @, ycreateCell(hwb, row, (short) 4, “工号”);# h, m7 W+ u+ S
//busBeans列表,为要显示的数据5 Q3 x; x  M# [* `" x. Z6 f4 Y4 K
for (int i = 1; i 《= busBeans.size(); i++) {9 ]2 f2 a/ t* G/ L/ h
HSSFRow row1 = sheet.createRow((short) i);
4 i. K1 W$ u. i- h. V" Q7 F: Jfor (int j = 0; j 《 5; j++) {
/ [$ H3 W4 h3 s: `$ tif (j == 0) {' h$ b. l4 I$ Q7 t
createCell(hwb, row1, (short) j, busBeans.get(i - 1).getStatName());8 E% h6 B6 V+ M6 @  c% k
} else if (j == 1) {
  x, ?# i' o# b7 v5 [; fcreateCell(hwb, row1, (short) j, busBeans.get(i - 1).getFunName());
5 q. [7 J; g; v} else if (j == 2) {; x( Q# ?- M  f! p
createCell(hwb, row1, (short) j, busBeans.get(i - 1).getCountNum());8 O. m2 z7 k0 W3 X" K7 s4 ?
} else if (j == 3) {6 Q& b7 F# Q' o
createCell(hwb, row1, (short) j, busBeans.get(i - 1).getMoney());
0 _. F1 X- h' _  K} else if (j == 4) {
& H; R8 F$ e2 `( Q9 A2 \9 g6 F2 B6 b8 N" G

% W1 F( Z- Z: q$ L$ N! fcreateCell(hwb, row1, (short) j, busBeans.get(i - 1).getLoginNo());
* ]0 C: W3 r% F* M" w6 e8 O}
/ r: O; M0 r: X* c- R}+ r5 B& {" Z( \8 w( l
hwb.write(os);
9 i9 t, l. J, Y6 g/ g. P/ ^' uos.close();' u3 o' w% o* U4 I+ R. h6 c
return “createSuccess”;
, S$ R9 P2 p/ r) x} catch (Throwable e) {' ~* x$ `, S$ i% @* @/ B
e.printStackTrace();- ?% m) L# V7 Z: |) y% e
this.forwardError(e.getMessage());2 ^( k7 b0 I! c3 L
}% C% Z+ x2 p; v% M- }6 z
return “fail”;9 a$ r0 k7 A& E+ S7 K
}
% g1 j7 J9 u& G7 l6 P, z; L& {public void createCell(HSSFWorkbook wb, HSSFRow row, short col, String val) {
  s/ m! e/ p/ n: T* b1 U1 n+ jHSSFCell cell = row.createCell(col);( @. L3 H+ Z* V, H9 q! h. W
cell.setCellValue(val);
( o" d" Y3 e0 X! P) m' @HSSFCellStyle cellStyle = wb.createCellStyle();# Y- j7 f! r" J( B  Y) w
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);2 U9 R1 s, @$ R# T* e$ h
cell.setCellStyle(cellStyle);" m" d4 V* Y8 M5 H( v
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-6-26 08:31 , Processed in 0.190759 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表