/**
* 導(dǎo)出報(bào)告
*/
@GetMapping("/report")
@ApiOperationSupport(order = 17)
@ApiOperation(value = "導(dǎo)出報(bào)告", 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(); }
}
}
}
下面是文件表格和entity對(duì)象文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-527000.html
@Data
@TableName("t_flange_joint")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "FlangeJoint對(duì)象", description = "法蘭節(jié)點(diǎn)表")
public class FlangeJoint extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 所屬項(xiàng)目
*/
@ApiModelProperty(value = "所屬項(xiàng)目")
private Long projectId;
/**
* 節(jié)點(diǎn)編號(hào)
*/
@ApiModelProperty(value = "節(jié)點(diǎn)編號(hào)")
private String jointNo;
/**
* 管線編號(hào)
*/
@TableField(updateStrategy = FieldStrategy.IGNORED)
@ApiModelProperty(value = "管線編號(hào)")
private String lineNo;
/**
* 包編號(hào)
*/
@ApiModelProperty(value = "包編號(hào)")
private String packageNo;
/**
* 圖紙編號(hào)
*/
@ApiModelProperty(value = "圖紙編號(hào)")
private String isometricNo;
/**
* 節(jié)點(diǎn)序列號(hào)
*/
@TableField(updateStrategy = FieldStrategy.IGNORED)
@ApiModelProperty(value = "節(jié)點(diǎn)序列號(hào)")
private String labelNo;
/**
* 節(jié)點(diǎn)類型
*/
@ApiModelProperty(value = "節(jié)點(diǎn)類型")
private String jointType;
/**
* pid
*/
@ApiModelProperty(value = "pid")
private String pid;
/**
* 法蘭尺寸
*/
@ApiModelProperty(value = "法蘭尺寸")
private String flangeSize;
/**
* 法蘭磅級(jí)
*/
@ApiModelProperty(value = "法蘭磅級(jí)")
private String flangeRating;
/**
* 法蘭材質(zhì)
*/
@TableField(updateStrategy = FieldStrategy.IGNORED)
@ApiModelProperty(value = "法蘭材質(zhì)")
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-527000.html
到了這里,關(guān)于導(dǎo)出excel,導(dǎo)出模板Excel(雙工作蒲)壓縮文件導(dǎo)出(即下載文件)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!