使用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);
        }
    }
}

生成后的文件:

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

相关推荐
Scott9999HH7 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社8 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海8 小时前
python 列表、元组、集合和字典
开发语言·python
二十雨辰9 小时前
[爬虫]-Urllib
爬虫·python
萧瑟余晖9 小时前
JDK 26 新特性详解
java·开发语言
马优晨10 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
玉鸯10 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_4462608511 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao11 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest