用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();
动态生成excel,并压缩
最新推荐文章于 2024-06-19 09:35:54 发布