阅读背景:

导出excel,导出模板Excel(双工作蒲)压缩文件导出(即下载文件)

来源:互联网 
/** * 导出报告 */ @GetMapping("/report") @ApiOperationSupport(order = 17) @ApiOperation(value = "导出报告", notes = "传入ids") public void exportReport(@RequestParam String ids, HttpServletResponse response) { List<FlangeJoint> joints = flangeJointService.listByIds(Func.toLongList(ids)); String uuid = UUID.randomUUID().toString(); ClassPathResource classPathResource = new ClassPathResource("templates/report-tpl.xlsx"); Path zipPath = Paths.get(pathProperties.getReport(), uuid+".zip"); File zipFile = zipPath.toFile(); if(zipFile.exists()){ zipFile.delete();//如果文件存在则先删除旧的文件 } List<File> files = null; try{ if(!zipFile.getParentFile().exists()){ Files.createDirectories(zipPath.getParent()); } // if(!zipFile.exists()){ // Files.createFile(zipPath); // } files = joints.stream() .filter(j -> j.getStatus() == 4) .map(joint -> { FlangeJointVO flangeJointVO = new FlangeJointVO(); BeanUtil.copy(joint, flangeJointVO); String jointNo = joint.getJointNo(); String packageNo = joint.getPackageNo(); String fileName = packageNo + "-" + jointNo + ".xlsx"; Path filePath = Paths.get(pathProperties.getReport(), uuid, fileName); File file = filePath.toFile(); InputStream is = null; FileOutputStream fos = null; try{ if(!file.getParentFile().exists()){ Files.createDirectories(filePath.getParent()); } if(!file.exists()){ Files.createFile(filePath); } is = classPathResource.getInputStream(); fos = new FileOutputStream(file); ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(is).build(); WriteSheet sheetE = EasyExcel.writerSheet(0).build(); WriteSheet sheetC = EasyExcel.writerSheet(1).build(); Long projectId = joint.getProjectId(); ProjectInfo projectInfo = projectInfoService.getById(projectId); if (projectInfo!=null){ flangeJointVO.setCustName(projectInfo.getCustName()); flangeJointVO.setProjectName(projectInfo.getName()); flangeJointVO.setProjectAddress(projectInfo.getAddress()); flangeJointVO.setDate(LocalDate.now().toString()); } String faceEast = flangeJointVO.getFaceEast(); String faceNorth = flangeJointVO.getFaceNorth(); String faceSouth = flangeJointVO.getFaceSouth(); String faceWest = flangeJointVO.getFaceWest(); String offsetEast = flangeJointVO.getOffsetEast(); String offsetNorth = flangeJointVO.getOffsetNorth(); String offsetSouth = flangeJointVO.getOffsetSouth(); String offsetWest = flangeJointVO.getOffsetWest(); if ("ACC".equals(faceEast) && "ACC".equals(faceNorth) && "ACC".equals(faceSouth) && "ACC".equals(faceWest)){ flangeJointVO.setFlangeFace("Good"); if ("ACC".equals(offsetEast) && "ACC".equals(offsetNorth) && "ACC".equals(offsetSouth) && "ACC".equals(offsetWest)){ flangeJointVO.setFlangeAlighment("Good"); }else { flangeJointVO.setFlangeAlighment("Fail"); } }else { flangeJointVO.setFlangeFace("Fail"); flangeJointVO.setFlangeAlighment("Fail"); } int sno = reportSnoService.getSno(projectId); flangeJointVO.setReportNo(String.format("%05d", sno)); Integer passPressure1 = flangeJointVO.getPassPressure1(); Integer passPressure2 = flangeJointVO.getPassPressure2(); flangeJointVO.setRightActualTorque(flangeJointVO.getActualTorque()); if (passPressure1!=null || passPressure2!=null){ flangeJointVO.setRightActualTorque(null); flangeJointVO.setBoltLoad(null); }else { flangeJointVO.setActualTorque(null); flangeJointVO.setPassPressure1(null); flangeJointVO.setPassPressure2(null); flangeJointVO.setBoltStress(null); } excelWriter.fill(flangeJointVO, sheetE); excelWriter.fill(flangeJointVO, sheetC); excelWriter.finish(); }catch(Exception e1){ e1.printStackTrace(); }finally{ if(fos != null) try{ fos.close(); }catch(Exception e2){e2.printStackTrace();} if(is != null) try{ is.close(); }catch(Exception e3){e3.printStackTrace();} } return file; }).collect(Collectors.toList()); ZipUtil.zipFile(files, zipPath.toFile().getAbsolutePath()); }catch(Exception e){ e.printStackTrace(); }finally { if(files != null){ for(File f : files){ if(f.exists()) f.delete(); } } Path dirPath = Paths.get(pathProperties.getReport(), uuid); if(dirPath.toFile().exists()){ dirPath.toFile().delete(); } } //下载文件 InputStream inStream = null; try { if(!zipFile.exists()){ return ; } inStream = new FileInputStream(zipFile); response.reset(); response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8")); response.setCharacterEncoding("UTF-8"); IoUtil.copy(inStream, response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 导出报告 */ @GetMapping("/report") @ApiOperatio



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: