Export excel, export template Excel (double work Po) compressed file export (ie download file)

/**
	 * 导出报告
	 */
	@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(); }
			}
		}
	}

Below is the file table and entity object

@Data
@TableName("t_flange_joint")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "FlangeJoint对象", description = "法兰节点表")
public class FlangeJoint extends BaseEntity {

    private static final long serialVersionUID = 1L;

    /**
     * 所属项目
     */
    @ApiModelProperty(value = "所属项目")
    private Long projectId;
    /**
     * 节点编号
     */
    @ApiModelProperty(value = "节点编号")
    private String jointNo;
    /**
     * 管线编号
     */
	@TableField(updateStrategy = FieldStrategy.IGNORED)
    @ApiModelProperty(value = "管线编号")
    private String lineNo;
    /**
     * 包编号
     */
    @ApiModelProperty(value = "包编号")
    private String packageNo;
    /**
     * 图纸编号
     */
    @ApiModelProperty(value = "图纸编号")
    private String isometricNo;
    /**
     * 节点序列号
     */
	@TableField(updateStrategy = FieldStrategy.IGNORED)
    @ApiModelProperty(value = "节点序列号")
    private String labelNo;
    /**
     * 节点类型
     */
    @ApiModelProperty(value = "节点类型")
    private String jointType;
    /**
     * pid
     */
    @ApiModelProperty(value = "pid")
    private String pid;
    /**
     * 法兰尺寸
     */
    @ApiModelProperty(value = "法兰尺寸")
    private String flangeSize;
    /**
     * 法兰磅级
     */
    @ApiModelProperty(value = "法兰磅级")
    private String flangeRating;
    /**
     * 法兰材质
     */
	@TableField(updateStrategy = FieldStrategy.IGNORED)
    @ApiModelProperty(value = "法兰材质")
    

Guess you like

Origin blog.csdn.net/weixin_42759398/article/details/131556945