EasyExcel - 动态头多Sheet示例记录

工具代码:

public static void generateDynamicHeadExcel(String filePath, List<String> sheetNames, Map<String,
            List<List<String>>> headMap, Map<String,List<List<Object>>> contentMap){

        ExcelWriter excelWriter = EasyExcel
                .write(filePath)
                .excelType(DEFAULT_EXCEL_TYPE)
                .autoCloseStream(Boolean.TRUE)
                .registerConverter(new LongStringConverter())
                .build();

        for (int i=0, j=sheetNames.size(); i<j; i++){
            String sheetName = sheetNames.get(i);
            List<List<String>> head = headMap.get(sheetName);
            List<List<Object>> content = contentMap.get(sheetName);

            WriteSheet writeSheet = EasyExcel.writerSheet(i, sheetName).head(head).build();
            excelWriter.write(content, writeSheet);
        }

        excelWriter.finish();
    }

测试代码:

public static void main(String[] args){
        List<String> sheetNames = Stream.of("sheet1", "sheet2").collect(Collectors.toList());

        Map<String, List<List<String>>> headMap = new HashMap<>();
        List<String> inList1 = Stream.of("tab1").collect(Collectors.toList());
        List<String> inList2 = Stream.of("tab2").collect(Collectors.toList());
        List<List<String>> list1 = Stream.of(inList1, inList2).collect(Collectors.toList());
        headMap.put(sheetNames.get(0), list1);
        List<String> inList3 = Stream.of("tab3").collect(Collectors.toList());
        List<String> inList4 = Stream.of("tab4").collect(Collectors.toList());
        List<List<String>> list2 = Stream.of(inList3, inList4).collect(Collectors.toList());
        headMap.put(sheetNames.get(1), list2);

        Map<String, List<List<Object>>> contentMap = new HashMap<>();
        List<List<Object>> list3 = new ArrayList<>();
        list3.add(Stream.of("content1", "content2").collect(Collectors.toList()));
        contentMap.put(sheetNames.get(0), list3);
        List<List<Object>> list4 = new ArrayList<>();
        list4.add(Stream.of("content3", "content4").collect(Collectors.toList()));
        contentMap.put(sheetNames.get(1), list4);

        EasyExcelUtil.generateDynamicHeadExcel("c://tempDir/123.xlsx", sheetNames, headMap, contentMap);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值