@Override
public void exportExcel(String applyId, String applyType, boolean isAddSeal, HttpServletResponse response) throws Exception {
// 查询物料信息
if (StringUtils.isEmpty(applyId)) {
throw new BusinessException(SysExceptionCode.SYS, "主键id为空");
}
if (StringUtils.isEmpty(applyType)) {
throw new BusinessException(SysExceptionCode.SYS, "申请类型为空");
}
ResultObject<SparepartApply> resultObject = this.sparepartApplyService.getDetailById(applyId);
SparepartApply dbSparepartApply = resultObject.getResult();
FlowProcessParams flowProcessParams= new FlowProcessParams();
flowProcessParams.setBusinessId(applyId);
ResultObject<List<ProgressNode>> flowInfoResults = flowService.progress(flowProcessParams);
List<ProgressNode> progressNodes = new ArrayList<>();
if(!CollectionUtils.isEmpty(flowInfoResults.getResult())){
progressNodes = flowInfoResults.getResult();
}
final int isDel = 0;
List<SparepartApplyDetailChange> sparePartDetailChange =sparepartApplyDetailChangeMapper.selectList(new LambdaQueryWrapper<SparepartApplyDetailChange>().eq(SparepartApplyDetailChange::getApplyId, applyId).eq(SparepartApplyDetailChange::getIsDel,"0"));
List<TConfigRepotFlow> tConfigs = tConfigRepotFlowMapper.selectList(new LambdaQueryWrapper<TConfigRepotFlow>().eq(TConfigRepotFlow::getVreportcode,"SparepartTemplate")
.eq(TConfigRepotFlow::getIsDel,isDel).orderByAsc(TConfigRepotFlow::getNsno));
JSONArray groupList = this.setGroupListData(dbSparepartApply,progressNodes,sparePartDetailChange,tConfigs);
if (groupList == null || groupList.size() == 0) {
throw new BusinessException(SysExceptionCode.SYS, "无可导出数据");
}
// 读取导出模版
ClassPathResource classPathResource = new ClassPathResource("/excel/SparepartTemplate.xlsx");
InputStream inputStream = classPathResource.getInputStream();
ServletOutputStream stream = response.getOutputStream();
//在这里做一些重构 为了给excel加船章
//todo 生成pdf文件和水印暂时先这样写,以后需改造 -- 2024-05-23 liux
String identifyNumberForTempFile = System.currentTimeMillis() + "";
FileOutputStream tmpExcelOutputStream = null;
FileInputStream tmpExcelInputStream = null;
tmpExcelOutputStream = new FileOutputStream(
String.format("D:\\ExportAddImage-%s.xlsx", identifyNumberForTempFile));
//创建内存输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//在这里做一些重构
// 创建写入数据对象
// 根据申请类型筛选条件判断设置文件名
String fileName = dbSparepartApply.getShipName() + "备件" + SparepartApplyTypeEnum.of(dbSparepartApply.getApplyType()).getName() + "表.xlsx";
setHeader(fileName, response);
ExcelWriter excelWriter = EasyExcel.write(byteArrayOutputStream).withTemplate(inputStream).inMemory(true).build();
WriteContext context = excelWriter.writeContext();
WriteSheet writeSheet0 = EasyExcel.writerSheet(0).build();
context.currentSheet(writeSheet0, WriteTypeEnum.FILL);
FillConfig config = FillConfig.builder().build();
config.setForceNewRow(true);
WriteWorkbookHolder writeWorkbookHolder = context.writeWorkbookHolder();
writeWorkbookHolder.setInMemory(true);
NestExcelWriteFillExecutor fillExecutor = new NestExcelWriteFillExecutor(context);
// 填充集合数据
context.currentSheet(writeSheet0, WriteTypeEnum.FILL);
fillExecutor.writeFill(new FillWrapper("groupList", groupList), config);
// 填充对象数据
Map<String, Object> simpleMap = new HashMap<>();
// 填报单位
simpleMap.put("shipName", StrIsNull(dbSparepartApply.getShipName()));
// 申请类型
String applyTypeVal = DomesticImportTypeEnum.acquireNameByCode(dbSparepartApply.getDomesticImportType())
+ SparepartApplyTypeEnum.of(dbSparepartApply.getApplyType()).getName();
simpleMap.put("applyType", StrIsNull(applyTypeVal));
// 申请部门
simpleMap.put("deptName", StrIsNull(dbSparepartApply.getDeptName()));
// 申请日期
simpleMap.put("applyDate", DateUtils.getDateString(dbSparepartApply.getApplyDate(), "yyyy年MM月dd日"));
// 制表人
simpleMap.put("userName", StrIsNull(CurrentUserUtils.getUserName()));
// 船舶负责人
// 机务
// 备件申请人
simpleMap.put("applicantName", StrIsNull(dbSparepartApply.getApplicantName()));
// 添加签名
excelWriter.fill(simpleMap, writeSheet0);
// 关闭流
excelWriter.finish();
// 是否加船章
Ship ship = shipMapper.selectById(dbSparepartApply.getShipId());
String shipSeal = ship.getShipSeal();
//读一下临时文件的流
tmpExcelInputStream = new FileInputStream(
String.format("D:\\ExportAddImage-%s.xlsx", identifyNumberForTempFile));
//将输出流拷贝到输入流
//创建内存输出流
// 获取内存输出流的字节数组
byte[] byteArray = byteArrayOutputStream.toByteArray();
// 使用获取到的字节数组创建内存输入流
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
if (isAddSeal) {
// simpleMap.put("image", handleImage(shipSeal));
//执行加船章的代码,调用工具类即可
File imageFile = new File("D:\\tu\\chaunzhang.jpg");
FileInputStream imageInputStream = new FileInputStream(imageFile);
byte[] bytes = IOUtils.toByteArray(imageInputStream);
// 调用工具类插入图片到Excel表格 第一个inputStream已经关闭了,不能用 tmpExcelInputStream
ExcelImageUtil.insertImageIntoExcel(byteArrayInputStream, bytes, stream);
}else {
IOUtils.copy(byteArrayInputStream, stream);
}
// 填充审批痕迹
// JSONObject flowData = dbSparepartApply.getFlowData();
//
// WriteSheet writeSheet1 = EasyExcel.writerSheet(1)
// .registerWriteHandler(new CustMergeStrategy())
// .registerWriteHandler(new SimpleCellWriteHandler())
// .build();
// excelWriter.fill(flowData, writeSheet1);
stream.close();
inputStream.close();
tmpExcelOutputStream.close();
tmpExcelOutputStream.close();
// todo 切记删除临时文件 try catch final删 如果是内存流就不用删除
}
内存流处理easyExcel的excel导出加图片
于 2024-06-18 09:11:11 首次发布