j2se导出多个excel 并压缩输出

导出多个excel表从压缩输出

这里的ZipOutputStream导入的是apache的jar包


public static void downloadZip(String[] caption,List<HSSFWorkbook> workbooks,HttpServletResponse response) throws Exception{
		response.reset();
		response.setHeader("Content-Disposition", "attachment;filename="+new String(("资源数据.zip").getBytes("UTF-8"),"iso-8859-1"));
		response.setContentType("application/octet-stream;charset=UTF-8");
		OutputStream  out=null;
		ZipOutputStream zip = null;
		try{
			
			out=response.getOutputStream();
			zip = new ZipOutputStream(out);
			
			
			for (int i=0; i<workbooks.size(); i++) {
				ZipEntry entry = new ZipEntry(caption[i]+".xls");
				zip.putNextEntry(entry);
				workbooks.get(i).write(zip);
			}
			zip.setEncoding("gbk");
			zip.flush();
			zip.close();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
			if(zip!=null)zip.close();
			if(out!=null)out.close();
		}
	}


千万记住设置编码  否则会出现中文乱码!!!!!!!!!!!!!!!

发布了31 篇原创文章 · 获赞 21 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章