使用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;
}
相关推荐
骆驼爱记录1 天前
Word一键批量添加图章
自动化·word·excel·wps·新人首发
码路星河1 天前
SpringBoot3实战:优雅实现Word文档动态生成与下载
开发语言·c#·word
Eiceblue1 天前
通过 C# 读取 Word 表格数据:高效解析 + 导出为 CSV/TXT
开发语言·c#·word
育种数据分析之放飞自我2 天前
obsidian的md文件导出word文件插件:Docx Exporter
word
进击的女IT2 天前
Java使用poi-tl实现word模版渲染文本/图片
java·数据库·word
A Everyman3 天前
Java 高效生成 Word 文档:poi-tl 的使用
java·pdf·word·poi-tl
AI英德西牛仔3 天前
ChatGPT和Gemini导出word排版
人工智能·ai·chatgpt·word·deepseek·ds随心转
reasonsummer4 天前
【办公类-142-03】20260304插班生word转长表EXCLE(3)从word表格按行导出列表,提取索引内容。写入EXCLE长表,另存有名字的文件名
word
深藏功yu名4 天前
Day22:RAG 王炸进阶!多格式文档 (PDF_Word)+ 多文档知识库搭建
人工智能·python·pycharm·langchain·pdf·word·rag
傻啦嘿哟4 天前
使用 Python 实现 Word 文档文本格式化全解析
开发语言·python·word