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

相关推荐
kyle~1 天前
CPU调度---协程
java·linux·服务器·数据库·c++20
会飞的小蛮猪1 天前
Skywalking运维之路(Skywalking服务搭建)
java·运维·监控
FogLetter1 天前
React Fiber 机制:让渲染变得“有礼貌”的魔法
前端·react.js
L.EscaRC1 天前
Redisson在Spring Boot中的高并发应用解析
java·spring boot·后端
他们叫我技术总监1 天前
从开发者视角深度评测:ModelEngine 与 AI 开发平台的技术博弈
java·人工智能·dubbo·智能体·modelengine
不想说话的麋鹿1 天前
「项目前言」从配置程序员到动手造轮子:我用Vue3+NestJS复刻低代码平台的初衷
前端·程序员·全栈
李辉20031 天前
Python逻辑运算符
java·网络·python
JunpengHu1 天前
esri-leaflet介绍
前端
摇滚侠1 天前
Spring Boot3零基础教程,StreamAPI 介绍,笔记98
java·spring boot·笔记
zm4351 天前
bpmn.js 自定义绘制流程图节点
前端·bpmn-js