批量下载多个Excle文档并打包
要求
当用户点击导出的时候 查看数据进行分类 有多少个类型 导出多少Excle文档
问题: 当有多个Excle文档的时候一次批量下载会被浏览器拦截 所以网上多数使用的都是导为zip压缩包下载
@Override public void exportRecordList(Integer sysUserId, HttpServletResponse response) throws Exception{ //获取数据 List<GasBottleMakeVo> recordList = putOnRecordMapper.getRecordList(sysUserId); if (null != recordList && recordList.size() > 0) { //通过类型进行分组 Map<String, List<GasBottleMakeVo>> listMap = recordList.stream().collect(Collectors.groupingBy(GasBottleMakeVo::getGallbladderPressure)); Map<String, Workbook> map = new HashMap<>(); //判断分组后数据是否为空 listMap.forEach((k, v) -> { ByteArrayOutputStream baos = new ByteArrayOutputStream();
//多个类型不同的表格 if ("test".equals(k)) { List<TemplateWorkSheet> workSheets = new ArrayList<>(); TemplateWorkSheet sheet1 = new TemplateWorkSheet(); sheet1.setSheetName(ExcelConstantUtil.HIGH); sheet1.setRowNames(ExcelConstantUtil.HIGH_ROWS); List<Object[]> follows = new ArrayList<>(); if (v != null && v.size() > 0) { for (int i = 0; i < v.size(); i++) { StringBuilder strs = new StringBuilder(""); GasBottleMakeVo gasBottleMakeVo = v.get(i);
// 给表格赋值 strs.append(i + 1).append(",").append(gasBottleMakeVo.getBottleCode()).append(",") .append("--"); follows.add(strs.toString().split(",")); } } sheet1.setDataList(follows); sheet1.setTitleNum(1); workSheets.add(sheet1); Workbook workbook = TemplateExcelUtils.createWorkBook(workSheets); map.put(k,workbook); } if ("类型判断值".equals(k)) { List<TemplateWorkSheet> workSheets = new ArrayList<>(); TemplateWorkSheet sheet1 = new TemplateWorkSheet(); sheet1.setSheetName(ExcelConstantUtil.HEAT); sheet1.setRowNames(ExcelConstantUtil.HEAT_ROWS); List<Object[]> follows = new ArrayList<>(); if (v != null && v.size() > 0) { for (int i = 0; i < v.size(); i++) { StringBuilder strs = new StringBuilder(""); GasBottleMakeVo gasBottleMakeVo = v.get(i); strs.append(i + 1).append(",").append(gasBottleMakeVo.getBottleCode()).append(",") .append("--").append(",").append("--"); follows.add(strs.toString().split(",")); } } sheet1.setDataList(follows); sheet1.setTitleNum(1); workSheets.add(sheet1); Workbook workbook = TemplateExcelUtils.createWorkBook(workSheets); map.put(k,workbook); //将表格存入map } }); //执行zip打包 compressFileToZipStream1(map,response); //删除数据 putOnRecordMapper.deleteRecord(sysUserId); } else { new Exception("未找到数据,导出失败"); } }
压缩文件流程
/** * 压缩单个excel文件的输出流 到zip输出流,注意zipOutputStream未关闭,需要交由调用者关闭之 * @param map excle文件的输入流 * @param response */ public static void compressFileToZipStream1( Map<String, Workbook> map ,HttpServletResponse response) throws Exception{ //文件的名称 String downloadFilename = "压缩包名称" + ".zip"; //转换中文否则可能会产生乱码 downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8"); // 指明response的返回对象是文件流 response.setContentType("application/octet-stream"); // 设置在下载框默认显示的文件名 response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename); ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); map.forEach((k,v)->{ try { byte[] buf = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); v.write(baos); byte[] content = baos.toByteArray(); ByteArrayInputStream is = new ByteArrayInputStream(content); InputStream bis = new BufferedInputStream(is); // Add ZIP entry to output stream. 设置Excle文件名 ZipEntry zipEntry = new ZipEntry(k + ".xlsx"); zos.putNextEntry(zipEntry); // Transfer bytes from the file to the ZIP file int len; while ((len = bis.read(buf)) > 0) { zos.write(buf, 0, len); } // Complete the entry //关闭excel输出流 v.close(); bis.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } }); zos.flush(); zos.close(); }
参考文档 : https://blog.csdn.net/houxuehan/article/details/89189820