多场景easyExcel根据模板填充excel文件(三)

有的时候,导出的表格很复杂,完全没必要在各种繁琐的格式设置中浪费精力,可以试试模板填充.可以很方便的完成复杂的表格导出.

目录

填充模板的场景:

一.基本填充

二.多数据填充

三.多数据填充+动态值

2.动态值自定义设定列

四.横向填充

五.多条数据多数据源

六.多数据源多sheet


数据源

    private List<TemplateFileDto> templateFileDtos() {
        List<TemplateFileDto> templateFileDtoList = new ArrayList<>();

        TemplateFileDto templateFileDto1 = new TemplateFileDto();
        templateFileDto1.setName("叶凡");
        templateFileDto1.setLife(9);
        templateFileDto1.setWeapon("万物母气鼎");
        templateFileDto1.setMaterialQuality("万物母气");
        templateFileDtoList.add(templateFileDto1);
        TemplateFileDto templateFileDto2 = new TemplateFileDto();
        templateFileDto2.setName("金乌大帝");
        templateFileDto2.setLife(2);
        templateFileDto2.setWeapon("道劫黄金钟");
        templateFileDto2.setMaterialQuality("道劫黄金");
        templateFileDtoList.add(templateFileDto2);
        TemplateFileDto templateFileDto3 = new TemplateFileDto();
        templateFileDto3.setName("青帝");
        templateFileDto3.setLife(2);
        templateFileDto3.setWeapon("混沌青莲");
        templateFileDto3.setMaterialQuality("青莲");
        templateFileDtoList.add(templateFileDto3);
        TemplateFileDto templateFileDto4 = new TemplateFileDto();
        templateFileDto4.setName("无始大帝");
        templateFileDto4.setLife(2);
        templateFileDto4.setWeapon("无始钟");
        templateFileDto4.setMaterialQuality("混沌石");
        templateFileDtoList.add(templateFileDto4);
        TemplateFileDto templateFileDto5 = new TemplateFileDto();
        templateFileDto5.setName("羽化大帝");
        templateFileDto5.setLife(2);
        templateFileDto5.setWeapon("羽化神图");
        templateFileDto5.setMaterialQuality("羽化青金");
        templateFileDtoList.add(templateFileDto5);
        return templateFileDtoList;
    }

填充模板的场景:

一.基本填充

代码

        String templateFileName = System.getProperty("user.dir") + ExAndImConstant.TEMPLATE_PATH + "template1.xlsx";

        TemplateFileDto templateFileDto = new TemplateFileDto();
        templateFileDto.setName("叶凡");
        templateFileDto.setLife(9);
        templateFileDto.setWeapon("万物母气鼎");
        templateFileDto.setMaterialQuality("万物母气");
        EasyExcel.write(path)
                .withTemplate(templateFileName)
                .sheet()
                .doFill(templateFileDto);
    }
TEMPLATE_PATH = "/src/main/resources/templateFile/";

模板

导出效果

可以看到,模板中\{name\}在导出时没有填充.

二.多数据填充

    public void TemplateFillList(String path) {
        String templateFileName = System.getProperty("user.dir") + ExAndImConstant.TEMPLATE_PATH + "template2.xlsx";
        List<TemplateFileDto> templateFileDtoList = this.templateFileDtos();
        EasyExcel.write(path)
                .withTemplate(templateFileName)
                .sheet()
                .doFill(templateFileDtoList);
        //好像这样省内存
/*        ExcelWriter excelWriter = EasyExcel.write(path).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(templateFileDtoList, writeSheet);
        excelWriter.fill(templateFileDtoList, writeSheet);
        excelWriter.finish();*/
    }

注释中是另一种实现方式,据说这种方式实现会节省内存.

模板

普通写法的效果

省内存写法的效果 两个fill,所以数据出现两次 

三.多数据填充+动态值

    public void TemplateFillListSum(String path) {
        String templateFileName = System.getProperty("user.dir") + ExAndImConstant.TEMPLATE_PATH + "template3.xlsx";
        List<TemplateFileDto> templateFileDtoList = this.templateFileDtos();
        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("date",new Date());
        valueMap.put("total",500);
        // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
        // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
        // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        ExcelWriter excelWriter = EasyExcel.write(path).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(templateFileDtoList, fillConfig,writeSheet);
        excelWriter.fill(valueMap, writeSheet);
        excelWriter.finish();
    }

如果list的数据不是最后一行,下面还有数据要填充,比如模板种的"统计",那么就需要

FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();

模板

导出效果

2.动态值自定义设定列

按照上面的案例,日期和统计这两行和最后一个表头是对齐的,如果想要自定义位置,可以用下面这个案例.

代码

 public void TemplateFillBigList(String path) {
        String templateFileName = System.getProperty("user.dir") + ExAndImConstant.TEMPLATE_PATH + "template4.xlsx";
        List<TemplateFileDto> templateFileDtoList = this.templateFileDtos();
        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("date",new Date());

        ExcelWriter excelWriter = EasyExcel.write(path).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(templateFileDtoList,writeSheet);
        excelWriter.fill(valueMap, writeSheet);

        // list 后面还有个统计 想办法手动写入
        // 这里偷懒直接用list 也可以用对象
        List<List<String>> rowList = new ArrayList<>();
        List<String> strList = new ArrayList<>();
        //todo
        strList.add(null);
        strList.add(null);
        strList.add(null);
        strList.add(null);
        strList.add(null);
        strList.add(null);
        strList.add(null);
        // 这个统计想放在第几列,这里就是第几个.例:我想把统计:1000放在第8列,前面strList.add(null)就放7个
        strList.add("统计:1000");
        rowList.add(strList);
        //这是write
        excelWriter.write(rowList,writeSheet);
        excelWriter.finish();
    }

模板

效果

注意,模板里已经没有"统计"这一行了,"统计:1000"写在了代码里.

四.横向填充

    public void TemplateFillListHorizontal(String path) {
        String templateFileName = System.getProperty("user.dir") + ExAndImConstant.TEMPLATE_PATH + "template5.xlsx";
        List<TemplateFileDto> templateFileDtoList = this.templateFileDtos();
        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("date",new Date());
        valueMap.put("total",500);
        // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
        // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
        // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
        FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
        ExcelWriter excelWriter = EasyExcel.write(path).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(templateFileDtoList, fillConfig,writeSheet);
        excelWriter.fill(valueMap, writeSheet);
        excelWriter.finish();
    }

 模板

效果

五.多条数据多数据源

代码

    public void TemplateFillListComplex(String path) {
        String templateFileName = System.getProperty("user.dir") + ExAndImConstant.TEMPLATE_PATH + "template6.xlsx";
        List<TemplateFileDto> templateFileDtoList = this.templateFileDtos();
        // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
        // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
        // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
        FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
        ExcelWriter excelWriter = EasyExcel.write(path).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(new FillWrapper("data1", templateFileDtoList), fillConfig, writeSheet);
        excelWriter.fill(new FillWrapper("data2",templateFileDtos1()),writeSheet);
        excelWriter.fill(new FillWrapper("data3",templateFileDtos2()),writeSheet);
        excelWriter.finish();
    }

多数据源

private List<TemplateFileDto1> templateFileDtos1() {
        List<TemplateFileDto1> list = new ArrayList<>();

        TemplateFileDto1 templateFileDto11 = new TemplateFileDto1();
        templateFileDto11.setName("狠人大帝");
        templateFileDto11.setSkill("吞天魔功");
        list.add(templateFileDto11);
        TemplateFileDto1 templateFileDto12 = new TemplateFileDto1();
        templateFileDto12.setName("西皇母");
        templateFileDto12.setSkill("西皇经");
        list.add(templateFileDto12);
        TemplateFileDto1 templateFileDto13 = new TemplateFileDto1();
        templateFileDto13.setName("无始大帝");
        templateFileDto13.setSkill("无始经");
        list.add(templateFileDto13);
        TemplateFileDto1 templateFileDto14 = new TemplateFileDto1();
        templateFileDto14.setName("虚空大帝");
        templateFileDto14.setSkill("虚空经");
        list.add(templateFileDto14);
        return list;
    }
    private List<TemplateFileDto2> templateFileDtos2() {
        List<TemplateFileDto2> list = new ArrayList<>();

        TemplateFileDto2 templateFileDto21 = new TemplateFileDto2();
        templateFileDto21.setName("龙纹黑金");
        templateFileDto21.setWeapon("龙纹黑金鼎");
        list.add(templateFileDto21);
        TemplateFileDto2 templateFileDto22 = new TemplateFileDto2();
        templateFileDto22.setName("羽化青金");
        templateFileDto22.setWeapon("羽化神图");
        list.add(templateFileDto22);
        TemplateFileDto2 templateFileDto23 = new TemplateFileDto2();
        templateFileDto23.setName("凰血赤金");
        templateFileDto23.setWeapon("恒宇炉");
        list.add(templateFileDto23);
        TemplateFileDto2 templateFileDto24 = new TemplateFileDto2();
        templateFileDto24.setName("道劫黄金");
        templateFileDto24.setWeapon("道劫黄金钟");
        list.add(templateFileDto24);
        TemplateFileDto2 templateFileDto25 = new TemplateFileDto2();
        templateFileDto25.setName("仙泪绿金");
        templateFileDto25.setWeapon("西皇塔");
        list.add(templateFileDto25);
        TemplateFileDto2 templateFileDto26 = new TemplateFileDto2();
        templateFileDto26.setName("神痕紫金");
        templateFileDto26.setWeapon("松塔");
        list.add(templateFileDto26);
        TemplateFileDto2 templateFileDto27 = new TemplateFileDto2();
        templateFileDto27.setName("永恒蓝金");
        templateFileDto27.setWeapon("麒麟杖");
        list.add(templateFileDto27);
        return list;
    }

 模板

效果

六.多数据源多sheet

代码

    public void TemplateFillListSheets(String path) {
        String templateFileName = System.getProperty("user.dir") + ExAndImConstant.TEMPLATE_PATH + "template7.xlsx";
        ExcelWriter excelWriter = EasyExcel.write(path).withTemplate(templateFileName).build();
        WriteSheet writeSheet0 = EasyExcel.writerSheet(0).build();
        WriteSheet writeSheet1 = EasyExcel.writerSheet(1).build();
        excelWriter.fill(templateFileDtos1(),writeSheet0);
        excelWriter.fill(templateFileDtos2(),writeSheet1);
        excelWriter.finish();
    }

 模板 sheet1

模板 sheet2

效果 

 git仓库:导入导出: 导入导出的实例

更多导出场景:多场景easyExcel导出excel文件(一)-CSDN博客

读取excel场景:多场景easyExcel读取excel文件(二)-CSDN博客

根据模板填充excel:多场景easyExcel根据模板填充excel文件(三)-CSDN博客

好的,以下是在 VSCode 中搭建 Spring Boot 项目的步骤: 1. 安装 Java 和 Maven,并将它们添加到系统环境变量中。 2. 在 VSCode 中安装 Java Extension Pack 扩展。 3. 在 VSCode 中创建一个空文件夹,作为项目的根目录。 4. 打开 VSCode 的终端,进入项目根目录,并执行以下命令创建一个空的 Maven 项目: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 5. 打开 pom.xml 文件,并添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 6. 在 VSCode 中打开 src/main/java/com/example/demo/DemoApplication.java 文件,并添加以下内容: ``` package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 7. 在 VSCode 中打开 src/main/resources/application.properties 文件,并添加以下内容: ``` server.port=8080 ``` 8. 在 VSCode 中打开终端,进入项目根目录,并执行以下命令启动 Spring Boot 项目: ``` mvn spring-boot:run ``` 9. 打开浏览器,访问 http://localhost:8080/,如果看到 "Whitelabel Error Page",则说明项目已经成功启动。 至此,你已经在 VSCode 中成功搭建了一个简单的 Spring Boot 项目。希望这些步骤能对你有所帮助!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值