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));
    }

四、效果展示

相关推荐
admiraldeworm4 小时前
Spring Boot + Spring AI 最小可运行 Demo
java·人工智能·ai
chenglin0164 小时前
ES_数据存储知识
java·服务器·elasticsearch
fs哆哆4 小时前
在VB.net中一维数组,与VBA有什么区别
java·开发语言·数据结构·算法·.net
johnZhangqi4 小时前
深圳大学-计算机信息管理课程实验 C++ 自考模拟题
java·开发语言·c++
David爱编程5 小时前
并发编程三大特性全解析:原子性、可见性、有序性,一文讲透!
java·后端
Sally璐璐5 小时前
Go语言变量声明与初始化详解
java·开发语言·golang
C4程序员6 小时前
北京JAVA基础面试30天打卡14
java·开发语言·面试
LGL6030A7 小时前
Java学习历程14——制作一款五子棋游戏(4)
java
22:30Plane-Moon8 小时前
项目1总结其三(图片上传功能)
ide·spring boot·vue
qq_589568108 小时前
javaweb开发笔记—— 前端工程化
java·前端