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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章