动态生成excel,并压缩

用JExcelApi和ant(ant中的ZipEntity类没有中文文件名的问题),下面是我的一个代码:
request.setCharacterEncoding( "utf8 ");
response.setCharacterEncoding( "utf8 ");
String   filename   =   "下载文件名 ";
//用Hibernate得到数据库中数据
Session   sess   =   OracleFactory.openSession();
List   list   =   new   ArrayList();
try{
list   =   sess.createCriteria(Data.class).list();
sess.flush();
}catch(Exception   ex){
ex.printStackTrace();
}finally{
OracleFactory.closeSession();
}
//写Excel
response.setContentType( "application/octet-stream ");
response.setHeader( "Content-Disposition ", "attachment;filename=/ " "+new   String((filename+ ".zip ").getBytes( "utf8 "), "ISO-8859-1 ")+ "/ " ");
OutputStream   responseOS   =   response.getOutputStream();
ZipOutputStream   zout   =   new   ZipOutputStream(new   BufferedOutputStream(responseOS));
zout.putNextEntry(new   ZipEntry(filename+ ".xls "));
WorkbookSettings   ws   =   new   WorkbookSettings();
ws.setLocale(new   Locale( "zh ",   "CN "));
WritableWorkbook   workbook   =   Workbook.createWorkbook(new   BufferedOutputStream(zout),   ws);
WritableSheet   sheet   =   workbook.createSheet( "sheet1 ",   0);
sheet.addCell(new   Label(   0,   0,   "col1 "));
sheet.addCell(new   Label(   1,0, "col2 "));
for(int   i=1;   i <=list.size();   i++){
Data   data   =   (Data)list.get(i-1);
sheet.addCell(new   Number(   0,   i,   data.getField1()));
sheet.addCell(new   Number(   1,   i,   data.getField2()));
}
workbook.write();
workbook.close();
//压缩文件
zout.closeEntry();
zout.flush();
zout.close();
response.flushBuffer();
response.setStatus(HttpServletResponse.SC_OK);
responseOS.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值