EasyPoi 使用$fe:模板语法生成Word动态行

1 Maven 依赖

html 复制代码
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.0.0</version>
</dependency> 

2 application.yml

html 复制代码
spring:
  main:
    allow-bean-definition-overriding: true

3 测试代码

java 复制代码
package com.xudongbase.poi.easypoi;

import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.entity.MyXWPFDocument;
import cn.hutool.core.io.resource.ClassPathResource;
import com.xudongbase.common.util.FileUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class WordTemplateTest {
    /**
     * 测试替换内容和动态行
     */
    @Test
    public void test() {
        try {
            //读文件
            ClassPathResource classPathResource = new ClassPathResource("/doc/word/EasyPoiWordTemplate.docx");
            File file = new File("D:/word/EasyPoiWordTemplateAfter.docx");
            XWPFDocument document = new MyXWPFDocument(classPathResource.getStream());
            Map<String, Object> contentMap = new HashMap<>();
            contentMap.put("xudongmaster1", "旭东怪1");
            contentMap.put("xudongmaster2", "旭东怪2");
            FileUtil.createNewFile(file);
            List<Map<String, Object>> tableDataList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                Map<String, Object> rowMap = new HashMap<>();
                rowMap.put("test", "测试" + i);
                rowMap.put("name", "名称" + i);
                tableDataList.add(rowMap);
            }
            contentMap.put("tableDataList", tableDataList);
            WordExportUtil.exportWord07(document, contentMap);
            //复制前两行
            document.write(new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4 模板

5 测试结果

注:

完整代码示例请前往 Gitee 查看 xudongbase 项目的 easypoi 分支。

xudongbase: 主要是项目中可以用到的共通方法,现有easyexcel分支在持续更新中。欢迎大家Star和提交Issues。easyexcel分支:批量设置样式,批量添加批注,批量合并单元格,设置冻结行和列,设置行高列宽,隐藏行和列,绑定下拉框数据,设置水印,插入图片 - Gitee.com

相关推荐
人道领域6 分钟前
Day | 11 【苍穹外卖统计业务的实现:含详细思路分析】
java·数据库·后端·苍穹外卖
xiaoye37085 小时前
Java 自动装箱 / 拆箱 原理详解
java·开发语言
YDS8296 小时前
黑马点评 —— 分布式锁详解加源码剖析
java·spring boot·redis·分布式
迷藏4947 小时前
**发散创新:基于 Rust的开源权限管理系统设计与实战**在现代软件架构中,**权限控制**早已不
java·开发语言·rust·开源
升鲜宝供应链及收银系统源代码服务7 小时前
《IntelliJ + Claude Code + Gemini + ChatGPT 实战配置手册升鲜宝》
java·前端·数据库·chatgpt·供应链系统·生鲜配送
daidaidaiyu7 小时前
Nacos实例一则及其源码环境搭建
java·spring
i建模7 小时前
将Edge浏览器的标签页从顶部水平排列**移至左侧垂直侧边栏
前端·edge
跟着珅聪学java7 小时前
js编写中文转unicode 教程
前端·javascript·数据库
小江的记录本7 小时前
【Redis】Redis全方位知识体系(附《Redis常用命令速查表(完整版)》)
java·数据库·redis·后端·python·spring·缓存
英俊潇洒美少年8 小时前
Vue3 深入响应式系统
前端·javascript·vue.js