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

相关推荐
妙码生花1 分钟前
两个月 59 篇 AI 开发日志 + Golang 商业级实战项目收工后,得来的 AI 使用心法-下
前端·后端·go
野生技术架构师10 分钟前
从向量检索到混合搜索优化:Spring AI 2.0.1 + pgvector RAG 实战
java·人工智能·spring
IT枫斗者枫哥28 分钟前
CompletableFuture 超时了,任务还在跑?用三个实验分清超时与取消
java
chushiyunen41 分钟前
mockito笔记
java·开发语言·笔记
Doubbbbbbble云1 小时前
基于空间局部性的排序算法性能重构思路4
java·重构·排序算法
计算机魔术师1 小时前
Beren Millidge、John Schulman、Charlie O'Neill 对谈递归自我改进离我们还有多远
前端
MetaLite1 小时前
Java 时间处理的两个坑:单位误判,半秒算成一秒
java·开发语言·前端
黄骨鱼骨1 小时前
DatI:给AI接入数据库,Agent时代的NL2SQL问数与多维表格应该怎么做
java·database·text2sql·nl2sql·chatbi·mcp·data agent
hiahiahia1231 小时前
SSE 到底是什么?
前端·人工智能
Bigger2 小时前
我把苹果发布会里的折叠屏,真的用网页做出来了
前端·ai编程·交互设计