EasyPoi 利用$fe:模板语法天生Word动态行

[复制链接]
发表于 2025-11-19 00:49:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1 Maven 依赖

  1. <dependency>
  2.     <groupId>cn.afterturn</groupId>
  3.     <artifactId>easypoi-spring-boot-starter</artifactId>
  4.     <version>4.0.0</version>
  5. </dependency>
复制代码
2 application.yml 

  1. spring:
  2.   main:
  3.     allow-bean-definition-overriding: true
复制代码
3 测试代码

  1. package com.xudongbase.poi.easypoi;
  2. import cn.afterturn.easypoi.word.WordExportUtil;
  3. import cn.afterturn.easypoi.word.entity.MyXWPFDocument;
  4. import cn.hutool.core.io.resource.ClassPathResource;
  5. import com.xudongbase.common.util.FileUtil;
  6. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  7. import org.junit.jupiter.api.Test;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. @SpringBootTest
  16. public class WordTemplateTest {
  17.     /**
  18.      * 测试替换内容和动态行
  19.      */
  20.     @Test
  21.     public void test() {
  22.         try {
  23.             //读文件
  24.             ClassPathResource classPathResource = new ClassPathResource("/doc/word/EasyPoiWordTemplate.docx");
  25.             File file = new File("D:/word/EasyPoiWordTemplateAfter.docx");
  26.             XWPFDocument document = new MyXWPFDocument(classPathResource.getStream());
  27.             Map<String, Object> contentMap = new HashMap<>();
  28.             contentMap.put("xudongmaster1", "旭东怪1");
  29.             contentMap.put("xudongmaster2", "旭东怪2");
  30.             FileUtil.createNewFile(file);
  31.             List<Map<String, Object>> tableDataList = new ArrayList<>();
  32.             for (int i = 0; i < 10; i++) {
  33.                 Map<String, Object> rowMap = new HashMap<>();
  34.                 rowMap.put("test", "测试" + i);
  35.                 rowMap.put("name", "名称" + i);
  36.                 tableDataList.add(rowMap);
  37.             }
  38.             contentMap.put("tableDataList", tableDataList);
  39.             WordExportUtil.exportWord07(document, contentMap);
  40.             //复制前两行
  41.             document.write(new FileOutputStream(file));
  42.         } catch (Exception e) {
  43.             e.printStackTrace();
  44.         }
  45.     }
  46. }
复制代码
4 模板


5 测试结果

 

 
注:

        完备代码示例请前往 Gitee 检察 xudongbase 项目的 easypoi 分支。
xudongbase: 重要是项目中可以用到的共通方法,现有easyexcel分支在连续更新中。欢迎各人Star和提交Issues。easyexcel分支:批量设置样式,批量添加表明,批量归并单位格,设置冻结行和列,设置行高列宽,隐蔽行和列,绑定下拉框数据,设置水印,插入图片 - Gitee.com
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表