欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 使用Poi-tl对word模板生成动态报告

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

2024/10/24 15:18:31 来源:https://blog.csdn.net/m0_47743175/article/details/141326479  浏览:    关键词:使用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);}}
}

生成后的文件:

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com