使用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;
}
相关推荐
gc_22991 天前
学习C#调用OpenXml操作word文档的基本用法(10:读取其它文档属性)
word·openxml·自定义属性·扩展属性
缺点内向1 天前
如何在 C# 中将 Word 文档转换为 EMF(增强型图元文件)
开发语言·c#·word·.net
secondyoung1 天前
Pandoc转换Word文档:使用Lua过滤器统一调整Pandoc文档中的图片和表格格式
经验分享·junit·word·lua·markdown·pandoc·mermaid
宋情写2 天前
poi-tl根据模板生成word文件
word
2501_930707782 天前
使用C#代码向 Word 文档添加文档属性
开发语言·c#·word
IT一氪3 天前
一款 AI 驱动的 Word 文档翻译工具
人工智能·word
东方佑3 天前
使用Python标准库将Word文档转换为HTML:深入解析.docx文件处理脚本
python·html·word
wtsolutions3 天前
Sheet-to-Doc 支持 JSON 和 JSONL 格式:批量生成 Word 文档的新方式
json·word·wtsolutions·sheet-to-doc
伟贤AI之路3 天前
原创分享:Word 转 Markdown - 精准还原 Word 格式、支持 LaTeX 公式与表格
word
开开心心_Every4 天前
优化C盘存储:自定义软件文档保存路径工具
java·网络·数据库·typescript·word·asp.net·excel