使用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;
}
相关推荐
Digitally18 小时前
如何在Windows、Mac和移动设备上永久删除Word文档
windows·macos·word
R-sz21 小时前
前端直接将页面 HTML 报表导出为 Word 文档,html转word
前端·html·word
DANGAOGAO1 天前
Word宏一键交叉引用
word·交叉引用
梅羽落1 天前
word改页码
word
傻啦嘿哟2 天前
使用 Python 管理 Word 节及页面布局设置
开发语言·python·word
梦因you而美2 天前
Python批量读取Word表格(全格式兼容:上下标+公式+字体样式)
python·自动化·word·办公自动化·提取word表格·omml格式
CodeCxil3 天前
基于Vue的在线Online Word文档编辑器-效果预览
前端·vue.js·word
CodeCxil3 天前
基于Vue的在线Online Word文档编辑器
vue.js·编辑器·word
诸葛大钢铁3 天前
如何将PDF以矢量图插入Word? 在线将PDF转为SVG格式
pdf·word·矢量图·pdf2svg
神仙别闹3 天前
基于 MATLAB 实现 Word 的信息隐藏算法
c#·word·xhtml