一、引入pom
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.0</version>
</dependency>
注意apache.poi版本要对应
NOTE: poi-tl 1.12.x 要求 POI 版本在 5.2.2+.
Apache POI已经进入5.0.0+时代,如果使用低版本的Apache POI,请查阅历史版本。
当前版本 1.12.0 Documentation,Apache POI5.2.2+,JDK1.8+
1.11.x Documentation,Apache POI5.1.0+,JDK1.8+
1.10.x Documentation,Apache POI4.1.2,JDK1.8+
1.10.3 Documentation,Apache POI4.1.2,JDK1.8+
1.9.x Documentation,Apache POI4.1.2,JDK1.8+
1.8.x Documentation,Apache POI4.1.2,JDK1.8+
1.7.x Documentation,Apache POI4.0.0+,JDK1.8+
1.6.x Documentation,Apache POI4.0.0+,JDK1.8+
1.5.x Documentation,Apache POI3.16+,JDK1.6+
V1.12.0版本作了一个不兼容的改动,升级的时候需要注意:
重构了PictureRenderData,改为抽象类,建议使用Pictures工厂方法来创建图片数据
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>5.2.2</version>
</dependency>
二、案例--动态表格的生成
创建两个文件夹,一个是用来存储模板文件,另一个是用来存储生成的文件
准备一个word模板,命名为test.docx,
注意,{{}}是官方指定的格式,也可以自定义。
[ ] 是可替换的属性 可以是多个
代码
@Data
public class People {
private String age;
private String index;
private String name;
private String note;
private ArrayList axisNoteList;
}
@Data
public class Student {
private String age;
private String index;
private String name;
private String sex;
private String sexs;
}
@Test
public void testOne(){
//模板地址
String templateFilePath = "C:\\Users\\18089\\Desktop\\";
//生成文件的保存地址
String destFilePath = "C:\\Users\\18089\\Desktop\\";
Map<String, Object> datas = new HashMap<String, Object>();
Student student1 = new Student();
student1.setAge("12");
student1.setIndex("1");
student1.setName("张三");
student1.setSex("男");
Student student2 = new Student();
student2.setAge("13");
student2.setIndex("2");
student2.setName("李四");
student2.setSex("男1");
List<Student> studentList = Arrays.asList(student1,student2);
People people1 = new People();
people1.setAge("13");
people1.setIndex("1");
people1.setName("lisi");
people1.setNote("测试");
People people2 = new People();
people2.setAge("14");
people2.setIndex("2");
people2.setName("lisi11");
people2.setNote("测试11");
List<People> peopleList = Arrays.asList(people1,people2);
datas.put("lists2",peopleList);
datas.put("lists",studentList);
// 行循环实例
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
//这里可以指定一个config类,用来指定一些规则,
Configure config = Configure.builder()
.bind("lists2",policy)
.bind("lists", policy).build();
XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "test.docx",config);
compile.render(datas);
try {
compile.writeToFile(destFilePath+"out_test.docx");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
生成文件
参考:
使用最全文章来源:https://www.toymoban.com/news/detail-540725.html
讲解最全文章来源地址https://www.toymoban.com/news/detail-540725.html
到了这里,关于poi-tl的使用(动态表格的生成)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!