javaWeb导出POI创建的多个excel的压缩文件

效果图

javaWeb导出POI创建的多个excel的压缩文件

 

		    @RequestMapping(value = "/poizip")
		    public void poizip(HttpServletResponse response) throws IOException {
		        //response 输出流
		        ServletOutputStream out = response.getOutputStream();
		        //压缩输出流---将response输出流填入压缩输出流
		        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
		        try {
		            for (int i = 0; i < 6; i++) {
		                //创建工作簿
		                HSSFWorkbook wb = new HSSFWorkbook();
		                HSSFSheet sheet = wb.createSheet("sheet" + i);
		                HSSFRow row = sheet.createRow(0);
		                HSSFCell cell = row.createCell(0);
		                cell.setCellValue("内容" + i);
		                response.setContentType("application/octet-stream; charset=utf-8");
		                response.setHeader("Content-Disposition", "attachment; filename=test.zip");
		                //重点开始,创建压缩文件,并进行打包
		                ZipEntry z = new ZipEntry(i + ".xls");
		                zipOutputStream.putNextEntry(z);
		                //写入一个压缩文件---最后写文件
		                wb.write(zipOutputStream);
		            }
		            zipOutputStream.flush();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            //注意关闭顺序,否则可能文件错误,先开后关
		            if (zipOutputStream != null) {
		                zipOutputStream.close();
		            }
		            if (out != null) {
		                out.close();
		            }
		        }
		    }

特此感谢

原博文地址     https://blog.****.net/cs373616511/article/details/80325458