easyexcel 动态表头

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;


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值