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

四、效果展示

相关推荐
小白天下第一21 小时前
java+三角测量(两个工业级)+人体3d骨骼关键点获取(yolov8+HRNET_w48_2d)
java·yolo·3d·三角测量
William Dawson21 小时前
Java 后端高频 20 题超详细解析 ①
java·开发语言
编程之升级打怪21 小时前
Java NIO的简单封装
java·开发语言·nio
wuxinyan12321 小时前
Java面试题46:一文深入了解JVM 核心知识体系
java·jvm·面试题
小江的记录本1 天前
【JEECG Boot】 《JEECG Boot 数据字典使用教程》(完整版)
java·前端·数据库·spring boot·后端·spring·mybatis
鲸渔1 天前
【C++ 变量与常量】变量的定义、初始化、const 与 constexpr
java·开发语言·c++
i220818 Faiz Ul1 天前
教育资源共享平台|基于springboot + vue教育资源共享平台系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·教育资源共享平台
玛卡巴卡ldf1 天前
【Springboot7】ApachePOI文件导入导出
java·spring boot·sql
编程大师哥1 天前
VSCode中如何搭建JAVA+MAVEN
java·vscode·maven
不会写DN1 天前
SQL 单表操作全解
java·服务器·开发语言·数据库·sql