当前位置: 首页 > news >正文

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

一、pom依赖问题:

<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版本(没有用到可以不引用)

<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:

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;}
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;
}
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;}
package com.fan.poi.entity;import lombok.Data;@Data
public class TaskDailyDataList {private String month;private String day;/*** 考勤人员*/private String Name;/*** 累计在岗时长*/private String totalOnSiteMinutes;
}

实现代码: 

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

生成后的文件:

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

相关文章:

  • 利用 Python 的包管理和动态属性获取(`__init__.py` 文件和 `getattr` 函数)特性来实现工厂方法模式
  • RHEL8 配置epel源
  • 深入探讨C语言中的高级指针操作
  • 生产环境中MapReduce的最佳实践
  • 微信小程序在不同移动设备上的差异导致原因
  • Startup-SBOM:一款针对RPM和APT数据库的逆向安全工具
  • Jenkins使用Publish Over SSH插件远程部署程序到阿里云服务器
  • vue3+ts+vite+pinia+element-plus搭建一个项目
  • 使用Docker-compose一键部署Wordpress平台
  • Bean对象生命周期流程图
  • Compose(2)声明式UI
  • 简简单单用用perf
  • Shell运算符
  • CDD数据库文件制作(五)——服务配置(0x19_DTC Code)
  • 基于深度学习的图像特征优化识别复杂环境中的果蔬【多种模型切换】
  • 【刷算法】求1+2+3+...+n
  • 3.7、@ResponseBody 和 @RestController
  • 4个实用的微服务测试策略
  • const let
  • Druid 在有赞的实践
  • Flannel解读
  • iOS编译提示和导航提示
  • js递归,无限分级树形折叠菜单
  • learning koa2.x
  • PHP变量
  • Python 基础起步 (十) 什么叫函数?
  • React的组件模式
  • scrapy学习之路4(itemloder的使用)
  • SQLServer之创建数据库快照
  • SQLServer之索引简介
  • ucore操作系统实验笔记 - 重新理解中断
  • web标准化(下)
  • 阿里云前端周刊 - 第 26 期
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 一起参Ember.js讨论、问答社区。
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • ​14:00面试,14:06就出来了,问的问题有点变态。。。
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • ​ubuntu下安装kvm虚拟机
  • ​一文看懂数据清洗:缺失值、异常值和重复值的处理
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #includecmath
  • ${factoryList }后面有空格不影响
  • $jQuery 重写Alert样式方法
  • (26)4.7 字符函数和字符串函数
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (回溯) LeetCode 40. 组合总和II
  • (学习总结16)C++模版2
  • (原)Matlab的svmtrain和svmclassify
  • (转)shell调试方法
  • .NET C#版本和.NET版本以及VS版本的对应关系