Java实现使用EasyExcel按模板导出文件

需求:导出excel文件要求头部标题内容过于复杂,在代码层面不好实现,直接提供模板,只需要将数据写入即可。

一、代码实现示例

实现数据写入模板代码如下:

java 复制代码
public void export(HttpServletResponse response) throws Exception {
        // 设置response参数
        String fileName = "导出文件名称.xlsx";
        response.addHeader("Access-Control-Expose-Headers", "content-disposition");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
        String template;
        // 根据环境设置模板路径
        String activeProfile = SpringContextUtil.getActiveProfile();
        if ("local".equals(activeProfile) || "@profileActive@".equals(activeProfile) || activeProfile == null ) {
            String path = FinVoucherCoverServiceImpl.class.getClass().getResource("/").getPath();
            //本地环境或者profileActive环境使用代码上的导出模板
            template = path.substring(0, path.indexOf("target")) + "src\\main\\resources\\templates\\excel\\fin_assetCard_fixed_import_template.xlsx";
        }else{
            //服务器层面的路径
            template = System.getProperty("user.dir") + "/config/fin_assetCard_fixed_import_template.xlsx";
        }

        try (OutputStream fileOut = response.getOutputStream()){
            ExcelWriter excelWriter = EasyExcel.write(fileOut).withTemplate(template).registerWriteHandler(GenerateUtil.getStrategy()).build();
            //写入数据格式1
            List<List<String>> typeList = new ArrayList<>();
            WriteSheet writeSheet1 = EasyExcel.writerSheet("sheet1").build();
            excelWriter.write(typeList, writeSheet1);

            //写入数据格式2
            //数据格式如下
            List<ExportVo> list = new ArrayList<>();
            WriteSheet writeSheet2 = EasyExcel.writerSheet("sheet2").build();
            excelWriter.fill(list, writeSheet2);
            excelWriter.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

二、写入格式及标识符

1、格式一

java 复制代码
List<ExportVo> list = new ArrayList<>();

以上数据类型数据,需要在模板中标明写入数据位置,格式如下:

使用{.字段名}的形式标识写入实体属性对应位置

2、格式二

java 复制代码
List<List<String>> typeList = new ArrayList<>();

以上格式类型数据不需要在模板中标识位置,会默认在写入的sheet页的第一行第一列开始写入,并以竖向方向写入数据。前提是第一行第一列的位置是空,没有其它值的情况,如有数据,会往下找到第一个为空的单元格,如第一列的1,2,3,4行单元格都有数据,写入数据时不会覆盖1,2,3,4列的数据,会从第一列第5行开始写入数据。

三、依赖相关

XML 复制代码
<dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>easyexcel</artifactId>
       <version>2.2.6</version>
</dependency>

这里使用的easyexcel依赖的版本是2.2.6,如使用版本更高,自行到官网查看使用版本写入数据方法是否更改,自行替换。

相关推荐
wifi___32 分钟前
全局异常处理的原理
java·开发语言
2601_963870205 小时前
【计算机毕业设计】基于Spring Boot的专科医院医疗管理系统
java·spring boot·课程设计
Bingo_BIG5 小时前
Java Spring 批量修改,实体、接口、方法的定义
java·spring
画中有画5 小时前
软件架构中质量属性(性能、安全、可扩展性)的权衡设计
java·运维·安全
Shaoxi Zhang6 小时前
JAVA学习笔记035——对象和JSON格式
java·笔记·学习
赵丙双6 小时前
CountDownLatch 源码分析
java·aqs·countdownlatch
余额瞒着我当琳6 小时前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reserve
java·开发语言·c++
thefool1122666 小时前
翻转二叉树
java
喜欢打篮球的普通人7 小时前
LLVM Backend Lowering 从入门到实战:把 IR 变成机器码的完整链路
android·java·数据库
FL16238631298 小时前
室内易燃物识别易燃评估室内易燃程度识别分割数据集labelme格式1015张85类别
java·服务器·前端