![](https://img2020.cnblogs.com/blog/1274088/202003/1274088-20200318164505799-1194558340.png)
//这里结合自己的业务数据 ids 是指需要导出的数据的id集合
/** * 导出所选台账 * * @return */ @ApiOperation(value = "导出所选台账") @GetMapping("/export") public void export(@ApiParam(value = "导出台账的id", required = true) int[] ids, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { //写入台账信息到excel ,下载文件到zip int id1 = ids[0]; Contract contract = contractService.selectByPrimaryKey(id1); String fileName = contract.getArchiveNo() + ".zip"; // 创建临时文件 File zipFile = null; try { zipFile = File.createTempFile(contract.getArchiveNo() + "等" + ids.length + "条记录", ".zip"); FileOutputStream f = new FileOutputStream(zipFile); /** * 作用是为任何OutputStream产生校验和 * 第一个参数是制定产生校验和的输出流,第二个参数是指定Checksum的类型 (Adler32(较快)和CRC32两种) */ CheckedOutputStream checkedOutputStream = new CheckedOutputStream(f, new Adler32()); // 用于将数据压缩成Zip文件格式 ZipOutputStream zos = new ZipOutputStream(checkedOutputStream); HSSFWorkbook workbook = new HSSFWorkbook(); //创建工作表对象 HSSFSheet sheet = workbook.createSheet(); //创建工作表的行 //设置第一行,从零开始 HSSFRow row = sheet.createRow(0); //第一行第一列为日期 这里的列 后面再加 TODO 在循环里设置对应的值就行 row.createCell(0).setCellValue("档号"); row.createCell(1).setCellValue("题名"); row.createCell(2).setCellValue("合同编号"); //设置sheet的Name workbook.setSheetName(0, "条目"); for (int k = 0; k < ids.length; k++) { int id = ids[k]; //写入数据到excel Contract contract2 = contractService.selectByPrimaryKey(id); HSSFRow row1 = sheet.createRow(k + 1); row1.createCell(0).setCellValue(contract2.getArchiveNo()); row1.createCell(1).setCellValue(contract2.getName()); row1.createCell(2).setCellValue(contract2.getCode()); //-------- 写入数据到excel end //以档号命名文件夹 zos.putNextEntry(new ZipEntry(contract2.getArchiveNo() + "\\")); String filePath = contract2.getArchiveNo() + "\\" + "文件" + "\\"; zos.putNextEntry(new ZipEntry(filePath)); List<Map> maps1 = contractFileService.selectByContractId(id); for (int i = 0; i < maps1.size(); i++) { Map m = maps1.get(i); String path = (String) m.get("path"); //name 后面追加 变成唯一名字 String name = path.substring(path.lastIndexOf("\\") + 1); //在挡号下面创建文件 zos.putNextEntry(new ZipEntry(filePath + name)); InputStream inputStream = new FileInputStream(path); int bytesRead = 0; // 向压缩文件中输出数据 while ((bytesRead = inputStream.read()) != -1) { zos.write(bytesRead); } inputStream.close(); } //写入数据到 档号\\附件 这个文件夹里 List<Map> maps2 = contractAttachmentService.selectByContractId(id); String attachmentPath = contract2.getArchiveNo() + "\\" + "附件" + "\\"; for (Map m : maps2) { String path = (String) m.get("path"); String name = path.substring(path.lastIndexOf("\\") + 1); zos.putNextEntry(new ZipEntry(attachmentPath + name)); InputStream inputStream = new FileInputStream(path); int bytesRead = 0; // 向压缩文件中输出数据 while ((bytesRead = inputStream.read()) != -1) { zos.write(bytesRead); } inputStream.close(); } // 当前文件写完 zos.closeEntry(); } zos.putNextEntry(new ZipEntry("条目.xls")); InputStream inputStream = new ByteArrayInputStream(workbook.getBytes()); int bytesRead = 0; // 向压缩文件中输出数据 while ((bytesRead = inputStream.read()) != -1) { zos.write(bytesRead); } inputStream.close(); zos.close(); String header = httpServletRequest.getHeader("User-Agent").toUpperCase(); if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) { fileName = URLEncoder.encode(fileName, "utf-8"); //IE下载文件名空格变+号问题 fileName = fileName.replace("+", "%20"); } else { fileName = new String(fileName.getBytes(), "ISO8859-1"); } httpServletResponse.reset(); httpServletResponse.setContentType("text/plain"); httpServletResponse.setContentType("application/octet-stream; charset=utf-8"); httpServletResponse.setHeader("Location", fileName); httpServletResponse.setHeader("Cache-Control", "max-age=0"); httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + fileName); FileInputStream fis = new FileInputStream(zipFile); BufferedInputStream buff = new BufferedInputStream(fis); BufferedOutputStream out = new BufferedOutputStream(httpServletResponse.getOutputStream()); byte[] car = new byte[4096]; int l = 0; while (l < zipFile.length()) { int j = buff.read(car, 0, 1024); l += j; out.write(car, 0, j); } // 关闭流 fis.close(); buff.close(); out.close(); // 删除临时文件 zipFile.delete(); } catch (IOException e1) { e1.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }