2025 Java EasyExcel 基于Excel模板填充数据 SpringBoot+Mybatis-Flex

一、模版文件【仅供参考】

二、pom依赖

XML 复制代码
        <!--easyExcel-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel-core</artifactId>
            <version>3.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel-support</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!-- POI导入导出 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.1</version>
        </dependency>

三、逻辑代码【仅供参考】

java 复制代码
     /**
     * 导出数据
     *
     * @return 所有数据
     */
    @GetMapping("export")
    public void export(HttpServletResponse response) throws IOException {
        //业务逻辑代码.....

        // 获取数据列表
        List<自己的实体类> exportDataList = 自己的Mapper.selectAll();

        // 6. 导出Excel
        exportToExcel(response, exportDataList, "示例表头标题", "示例文件名称");
    }

    /**
     * 导出数据到Excel
     *
     * @param response
     * @param data         导出的数据源
     * @param templateName 表头标题
     * @param fileName     文件名称
     * @throws IOException
     */
    private void exportToExcel(HttpServletResponse response, List<自己的实体类> data, String templateName, String fileName) throws IOException {
        InputStream templateFile = Thread.currentThread()
                .getContextClassLoader()
                .getResourceAsStream("template/template.xlsx");

        ExcelWriter excelWriter = null;

        try {
            // 设置响应头
            setupResponseHeaders(response, fileName);

            excelWriter = EasyExcel.write(response.getOutputStream())
                    .withTemplate(templateFile)
                    .build();

            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
            // 构造标题填充数据
            Map<String, String> titleFillData = new HashMap<>();
            titleFillData.put("templateName", templateName);
            // 先填充标题占位符
            excelWriter.fill(titleFillData, writeSheet);
            // 填充数据
            excelWriter.fill(data, fillConfig, writeSheet);


        } catch (Exception e) {
            handleExportError(response, e);
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
            if (templateFile != null) {
                templateFile.close();
            }
        }
    }


    /**
     * 设置响应头
     *
     * @param response
     * @param fileName 文件名称
     * @throws IOException
     */
    private void setupResponseHeaders(HttpServletResponse response, String fileName)
            throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }

    /**
     * 处理导出错误
     */
    private void handleExportError(HttpServletResponse response, Exception e)
            throws IOException {
        response.reset();
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        Map<String, String> map = new HashMap<>();
        map.put("status", "failure");
        map.put("message", "下载文件失败: " + e.getMessage());
        response.getWriter().println(JSON.toJSONString(map));
    }

四、效果展示

相关推荐
a努力。1 天前
宇树Java面试被问:方法区、元空间的区别和演进
java·后端·面试·宇树科技
2501_916766541 天前
【面试题1】128陷阱、==和equals的区别
java·开发语言
a程序小傲1 天前
蚂蚁Java面试被问:注解的工作原理及如何自定义注解
java·开发语言·python·面试
幽络源小助理1 天前
SpringBoot+Vue摄影师分享社区源码 – Java项目免费下载 | 幽络源
java·vue.js·spring boot
0和1的舞者1 天前
《软件测试分类指南:8 大维度 + 核心要点梳理》
java·软件测试·单元测试·测试·黑盒测试·白盒测试·测试分类
TAEHENGV1 天前
创建目标模块 Cordova 与 OpenHarmony 混合开发实战
android·java·开发语言
是一个Bug1 天前
如何阅读JDK源码?
java·开发语言
+VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue健身房管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Ledison71 天前
Springboot 3.5.7 + Springcloud 2025 升级记录
java
没有bug.的程序员1 天前
熔断、降级、限流:高可用架构的三道防线
java·网络·jvm·微服务·架构·熔断·服务注册