
//這里結合自己的業務數據 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(); }