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


    先看模板和导出的效果
    模板
    在这里插入图片描述
    效果
    在这里插入图片描述
    根据模板循环生成表格,每个表格再循环填充数据,也就是两层循环,第一层循环是学生学期信息,第二层循环是学生的成绩数据。

    第一个循环

    {{?listTable}} 
    {{/}}
    
    • 1
    • 2

    第二个循环

    {{reportList}}
    表格中[]是具体的属性,比如[displayName]表示课程名称
    
    • 1
    • 2

    核心代码

    maven

    <dependency>
        <groupId>com.deepoovegroupId>
        <artifactId>poi-tlartifactId>
        <version>1.10.5version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    @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);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    学业课程数据vo

    
    /**
     * 学业课程数据
     * @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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
  • 相关阅读:
    八股总结(六):Android基础:四大组件与UI控件
    python--第五章 python字典&&函数
    一.使用qt creator 设计显示GUI
    基于SSM的医院在线挂号预约系统的设计与实现
    Go实现日志2——支持结构化和hook
    剑指offer 16. 在O(1)时间删除链表结点
    day22集合01
    技术杂记:nginx进程的view和kill / linux命令
    面试算法 环形链表的判定
    Java并发-多线程售票案例
  • 原文地址:https://blog.csdn.net/frankcheng5143/article/details/133692150