效果图

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.csdn.net/cs373616511/article/details/80325458

 

相关文章:

  • 2021-09-30
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
  • 2021-09-01
  • 2021-08-11
猜你喜欢
  • 2021-12-15
  • 2021-11-28
  • 2022-12-23
  • 2021-08-26
  • 2021-12-26
  • 2022-12-23
  • 2021-06-24
相关资源
相似解决方案