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

四、效果展示

相关推荐
猫头虎18 分钟前
解决升级IDEA2025.2后,每次打开Maven项目爆红的问题:Windows和Mac解决方案
java·ide·macos·maven·intellij-idea·idea·intellij idea
NFG89C21 分钟前
Adobe Lightroom Classic 2025解锁版 (专业照片管理)
java·adobe·工具
蒙奇D索大37 分钟前
【计算机网络】408计算机网络高分指南:物理层编码与调制技术精讲
java·前端·学习·计算机网络
2401_8370885041 分钟前
JedisConnectionFactory 连接池工厂类
mybatis
豐儀麟阁贵42 分钟前
5.2 类
java·开发语言
九皇叔叔44 分钟前
Java循环结构全解析:从基础用法到性能优化(含经典案例)
java·开发语言·python
漫漫不慢.1 小时前
蓝桥杯-16955 岁月流转
java·jvm·蓝桥杯
JanelSirry1 小时前
如何查看java死锁?具体怎么做,怎么避免
java·开发语言
yinghuaqipao1 小时前
面向对象——设计模式(创建型)
android·java·设计模式
Jing_jing_X1 小时前
Spring 自动注入是怎么实现的?从 @Component 到 @Autowired 的完整流程
java·后端·spring