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

相关推荐
PineappleCoder1 天前
性能数据别再瞎轮询了!PerformanceObserver 异步捕获 LCP/CLS,不卡主线程
前端·性能优化
PineappleCoder1 天前
告别字体闪烁 / 首屏卡顿!preload 让关键资源 “高优先级” 提前到
前端·性能优化
45288655上山打老虎1 天前
C++完美转发
java·jvm·c++
Seven971 天前
查找算法
java
m0_471199631 天前
【vue】通俗详解package-lock文件的作用
前端·javascript·vue.js
GIS之路1 天前
GDAL 读取KML数据
前端
今天不要写bug1 天前
vue项目基于vue-cropper实现图片裁剪与图片压缩
前端·javascript·vue.js·typescript
毕设源码-朱学姐1 天前
【开题答辩全过程】以 公务员考试在线测试系统为例,包含答辩的问题和答案
java
serendipity_hky1 天前
【SpringCloud | 第2篇】OpenFeign远程调用
java·后端·spring·spring cloud·openfeign