使用poi-tl循环导出word报表

先看模板和导出的效果

模板

效果

根据模板循环生成表格,每个表格再循环填充数据,也就是两层循环,第一层循环是学生学期信息,第二层循环是学生的成绩数据。

第一个循环

复制代码
{{?listTable}} 
{{/}}

第二个循环

复制代码
{{reportList}}
表格中[]是具体的属性,比如[displayName]表示课程名称

核心代码

maven

xml 复制代码
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.10.5</version>
</dependency>
java 复制代码
@Test
public void export() throws IOException {

    // 模板文件
    ClassPathResource classPathResource = new ClassPathResource("/doc/template.docx");
    InputStream inputStream = classPathResource.getInputStream();

    Map<String, Object> map = new HashMap<>();

    String str = FileUtil.readFileByPath("C:\\Users\\chenggaowei\\Desktop\\score.json");

    List<StudentCourseVO> studentList = new ArrayList<>();
    // 模拟写入多个学生的数据
    for (int i = 0; i < 3; i++) {

        StudentCourseVO vo = new StudentCourseVO();

        vo.setStudentName("张三" + (i + 1));
        vo.setPeriodName("2023-2024学年上学期");

        List<ReportField> reportList = JSONObject.parseArray(str, ReportField.class);
        vo.setReportList(reportList);

        studentList.add(vo);
    }
    // 循环的数据,listTable和模板对应
    map.put("listTable", studentList);

    LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
    // reportList和模板对应
    Configure config = Configure.builder().bind("reportList", policy).build();
    XWPFTemplate template = XWPFTemplate.compile(inputStream, config).render(map);

    File outputFile = FileUtil.createTempFile("out.docx");
    template.writeToFile(outputFile.getAbsolutePath());

    FileUtil.openFolder(outputFile);

}

学业课程数据vo

java 复制代码
/**
 * 学业课程数据
 * @author chenggaowei
 * @date 2023/10/8
 */
@Data
public class StudentCourseVO {
    /**
     * 学生id
     */
    private String studentId;
    /**
     * 学生姓名
     */
    private String studentName;
    /**
     * 学期id
     */
    private String periodId;
    /**
     * 学期名称
     */
    private String periodName;
    /**
     * 成绩数据
     */
    private List<ReportField> reportList;
}
相关推荐
黑咩狗夜.cm2 天前
(aspose.words .net)内容分别固定在一行左右俩端
c#·word·.net
E_ICEBLUE2 天前
如何提取 Word 文档中的表格并导出为 Excel(Python 教程)
python·word·excel
IT策士2 天前
Python Word操作:从入门到精通
python·c#·word
aaaffaewrerewrwer2 天前
一个功能非常完整的在线 Word Search Puzzle(单词搜索)网站推荐:支持自定义出题、打印与多语言
游戏·word·个人开发
asdzx673 天前
告别手工复制:用 C# 轻松合并多份 Word
c#·word
有趣灵魂3 天前
Java Spring Boot根据Word模板和动态数据生成Word文件
java·spring boot·word·apache
05候补工程师3 天前
【Python实战】告别杂乱脚本!基于SOLID原则与策略模式的 PDF转Word 批量处理系统
python·设计模式·pdf·word·策略模式
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ4 天前
通过java后端代码来实现给word内容补充格式文本内容控件,以及 设置控件的标记和标题
java·c#·word
asdzx675 天前
使用 C# 从 URL 下载 Word 文档
开发语言·c#·word
VBAMatrix6 天前
deepseek-v4正式接入Excel,一键生成财务分析报告
word·excel·审计·财务分析·deepseek·会计师事务所·tb工具箱