public void buildExcel(HttpServletResponse response, List<EquipmentInfoVO> equipmentInfos, List<EquipmentServerVO> urlAndPort, SmcStationDTO smcStationDTO,List<QCProtocolVO> qcProtocolVOS) throws IOException, WriterException {
ByteArrayOutputStream byteArrayOutputStream = qrCodeGenBuilder.asStream();
if (gunIndex == 1) {
qrCodeDataDto.setQrCode(qrCode);
}
if (gunIndex == 2) {
qrCodeDataDto.setQrCode2(qrCode);
}
if (gunIndex == 3) {
qrCodeDataDto.setQrCode3(qrCode);
}
if (gunIndex == 4) {
qrCodeDataDto.setQrCode4(qrCode);
}
if (gunIndex == 5) {
qrCodeDataDto.setQrCode5(qrCode);
}
if (gunIndex == 6) {
qrCodeDataDto.setQrCode6(qrCode);
}
if (gunIndex == 7) {
qrCodeDataDto.setQrCode7(qrCode);
}
if (gunIndex == 8) {
qrCodeDataDto.setQrCode8(qrCode);
}
gunIndex++;
}
qrCodeDataDtos.add(qrCodeDataDto);
}
Integer integer = equipmentInfos.stream().map(EquipmentInfoVO::getGunIndex).max(Comparator.naturalOrder()).orElse(null);
response.reset();
SimpleDateFormat yyyymmdd = new SimpleDateFormat("yyyyMMdd");
String format = yyyymmdd.format(new Date());
String fileName = URLEncoder.encode(smcStationDTO.getStationName() + "刷桩物料" + format + ".xlsx", "UTF-8");
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
OutputStream os = null;
try {
os = response.getOutputStream();
} catch (Exception e) {
}
List<QrCodeDataDto> collect = qrCodeDataDtos.stream().sorted(Comparator.comparing(QrCodeDataDto::getCreateTime).reversed()).collect(Collectors.toList());
ExcelWriter excelWriter = EasyExcel
.write().autoCloseStream(Boolean.TRUE)
.registerConverter(new LongStringConverter())
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(25))
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short) 15, (short) 150))
.registerWriteHandler(new CustomHeadCellWriteHandler())
.file(os)
.build();
// sheet
WriteSheet sheet = EasyExcel.writerSheet(1, "Sheet1").build();
// table
WriteTable head = EasyExcel.writerTable(0).head(buildHead(integer)).needHead(Boolean.TRUE).build();
WriteTable data = EasyExcel.writerTable(1).needHead(Boolean.FALSE).build();
excelWriter.write(new ArrayList(), sheet, head);
excelWriter.write(collect, sheet, data);
excelWriter.finish();
}
private List<List<String>> buildHead(Integer qrCodes) {
List<List<String>> head = new ArrayList<>();
List<String> station = Lists.newArrayList("站点");
List<String> serialNum = Lists.newArrayList("序列号");
List<String> assetCode = Lists.newArrayList("资产码");
List<String> createTime = Lists.newArrayList("创建日期");
List<String> location = Lists.newArrayList("正式环境地址");
List<String> port = Lists.newArrayList("端口号");
head.add(station);
head.add(serialNum);
head.add(assetCode);
head.add(createTime);
head.add(location);
head.add(port);
for (int i = 0; i < qrCodes; i++) {
head.add(Lists.newArrayList("0" + String.valueOf(i + 1) + "枪二维码"));
}
;
return head;
}
easyexcel 动态表头
最新推荐文章于 2024-09-09 09:21:02 发布