使用Poi-tl对word模板生成动态报告

一、pom依赖问题:

XML 复制代码
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.2</version>
</dependency>

使用 poi-tl 的 1.12.2版本,如果使用了poi依赖,则必须使用 poi的5.2.2版本(没有用到可以不引用)

XML 复制代码
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>

注意:如果还需要使用easyPoi 4.4.0 那这样是不行的,因为poi的5.2.2版本不兼容easyPoi ,只能将poi的版本降低到4.1.1,这样 poi-tl 就不能使用了,这里只能三者取其二。

二、具体实现:

1. word模板文件结构,根据实际情况修改:

这里我们使用了基本字符、表格,还有集合里面嵌套集合。

2. 相关代码:

基本entity:

java 复制代码
package com.fan.poi.entity;

import lombok.Data;

import java.util.List;

@Data
public class PersonDuty {
    private String startMonth;
    private String startDay;
    private String endMonth;
    private String endDay;
    /**
     * 到岗总人数
     */
    private String dgSum;
    /**
     * 未到岗总人数
     */
    private String ndgNum;
    /**
     * 到岗率
     */
    private String dgRate;

    private String dutyName;


    private List<ReportTable> tableList1;

    private List<ReportDataTable> monList;

}
java 复制代码
package com.fan.poi.entity;

import lombok.Data;

@Data
public class ReportTable {
    /**
     * 所属部门
     */
    private String supUnitName;
    /**
     * 到岗天数
     */
    private String supCount;
    /**
     * 未到岗数量
     */
    private String noSupCount;
    /**
     * 部门到岗率
     */
    private String syoSum;
    /**
     *  人员到岗总时长
     */
    private String allSupDuration;
}
java 复制代码
package com.fan.poi.entity;

import lombok.Data;

import java.util.List;

@Data
public class ReportDataTable {
    private String name;
    /**
     * 所属部门
     */
    private String supUnitName;
    /**
     * 共到岗天数
     */
    private String supCount;
    /**
     * 未到岗天数
     */
    private String noSupCount;
    /**
     * 到岗率
     */
    private String syoSum;
    /**
     * 累计到岗时长
     */
    private String allSupDuration;
    /**
     * 详细情况
     */
    private List<TaskDailyDataList> taskDailyDataList;

}
java 复制代码
package com.fan.poi.entity;

import lombok.Data;

@Data
public class TaskDailyDataList {
    private String month;

    private String day;
    /**
     * 考勤人员
     */
    private String Name;
    /**
     * 累计在岗时长
     */
    private String totalOnSiteMinutes;
}

实现代码:

java 复制代码
package com.fan.poi;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.fan.poi.entity.PersonDuty;
import com.fan.poi.entity.ReportDataTable;
import com.fan.poi.entity.ReportTable;
import com.fan.poi.entity.TaskDailyDataList;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 动态生成word模板
 * @author fan
 */
public class FileUtil {
    public static void main(String[] args) {
        exportWord("D:\\生成前的模板文件.docx", "D:\\");
    }

    public static void exportWord( String sorFilePath, String TarFilePath) {
        try {

            PersonDuty personDuty = new PersonDuty();
            // 模拟数据
            personDuty.setDutyName("张三");
            personDuty.setStartDay("2023-05-01");
            personDuty.setEndDay("2023-05-07");
            personDuty.setStartMonth("2023-05");
            personDuty.setEndMonth("2023-05");
            personDuty.setDgSum("5");
            personDuty.setDgRate("90");
            personDuty.setNdgNum("1");
            List<ReportTable> tableList1 = new ArrayList<>();
            ReportTable reportTable = new ReportTable();
            reportTable.setSyoSum("100");
            reportTable.setSupUnitName("测试部门");
            reportTable.setSupCount("2");
            reportTable.setNoSupCount("0");
            reportTable.setAllSupDuration("10");
            tableList1.add(reportTable);
            ReportTable reportTable1 = new ReportTable();
            reportTable1.setSyoSum("33%");
            reportTable1.setSupUnitName("开发部门");
            reportTable1.setSupCount("2");
            reportTable1.setNoSupCount("1");
            reportTable1.setAllSupDuration("10");
            tableList1.add(reportTable1);
            personDuty.setTableList1(tableList1);
            List<ReportDataTable> monList = new ArrayList<>();
            ReportDataTable reportDataTable = new ReportDataTable();
            reportDataTable.setName("张三");
            reportDataTable.setSupUnitName("测试部");
            reportDataTable.setSupCount("5");
            reportDataTable.setNoSupCount("0");
            reportDataTable.setSyoSum("100%");
            reportDataTable.setAllSupDuration("40");
            List<TaskDailyDataList> taskDailyDataList = new ArrayList<>();
            TaskDailyDataList taskDailyDataList1 = new TaskDailyDataList();
            taskDailyDataList1.setName("张三");
            taskDailyDataList1.setDay("2023-05-01");
            taskDailyDataList1.setMonth("2023-05");
            taskDailyDataList1.setTotalOnSiteMinutes("40");
            TaskDailyDataList taskDailyDataList2 = new TaskDailyDataList();
            taskDailyDataList2.setName("李四");
            taskDailyDataList2.setDay("2021-05-01");
            taskDailyDataList2.setMonth("2021-05");
            taskDailyDataList2.setTotalOnSiteMinutes("40");
            taskDailyDataList.add(taskDailyDataList1);
            taskDailyDataList.add(taskDailyDataList2);
            reportDataTable.setTaskDailyDataList(taskDailyDataList);
            monList.add(reportDataTable);
            personDuty.setMonList(monList);

            String targetFileName = "生成后的文件.docx";

            //modelFileName是模板路径/模板文件名
            String modelFileName = "D:";
            File file = new File(modelFileName);
            if (!file.exists()) {
                System.out.println("模板文件不存在:" + modelFileName);
                throw new RuntimeException("模板文件不存在");
            }
            //生成报告,对于集合,只要personDuty包含对应的集合即可,有嵌套也会自动替换
            Configure config = Configure.builder()
                    .bind("tableList1", new LoopRowTableRenderPolicy())
                    .bind("monList", new LoopRowTableRenderPolicy())
                    .build();

            XWPFTemplate template = XWPFTemplate.compile(sorFilePath, config).render(personDuty);

            //判断目标路径是否存在
            File directory = new File(TarFilePath);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            //定义输出路径
            String targetPath = TarFilePath + targetFileName;
            template.writeToFile(targetPath);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

生成后的文件:

这里主要是简单的把几种场景实现一下,项目中遇到的问题,还需我们自定义拼接逻辑。

相关推荐
编程版小新几秒前
C++初阶:STL详解(四)——vector迭代器失效问题
开发语言·c++·迭代器·vector·迭代器失效
c4fx20 分钟前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
鸽芷咕44 分钟前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Jhxbdks1 小时前
C语言中的一些小知识(二)
c语言·开发语言·笔记
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
子午1 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
Fan_web1 小时前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机