在文档的任何地方做任何事情(Do Anything Anywhere)是poi-tl的星辰大海。
1.入门
1.添加依赖
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.7.3</version>
</dependency>
2.定义模板
3.代码
import com.deepoove.poi.XWPFTemplate;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Map<String, Object> data = new HashMap<>();
data.put("name", "司天宏");
data.put("start_time", "2020-09-17");
XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
.render(data);
FileOutputStream out;
out = new FileOutputStream("F:/template2.docx");
template.write(out);
out.flush();
out.close();
template.close();
}
}
效果
4.数据也可以是对象
import com.deepoove.poi.XWPFTemplate;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Map<String, Object> data = new HashMap<>();
Sth sth = new Sth("司天宏", "武汉");
data.put("sth",sth);
XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
.render(data);
FileOutputStream out;
out = new FileOutputStream("F:/template2.docx");
template.write(out);
out.flush();
out.close();
template.close();
}
}
class Sth {
private String name;
private String addr;
public Sth(String name, String addr) {
this.name = name;
this.addr = addr;
}
}
template中这么取值
代码运行效果
二.标签
2.1.文本
模板中这样写
代码
Map<String, Object> data = new HashMap<>();
data.put("author", new TextRenderData("000000", "Sayi"));
// 超链接
data.put("link",
new HyperLinkTextRenderData("website", "http://deepoove.com"));
// 锚点
data.put("anchor",
new HyperLinkTextRenderData("anchortxt", "anchor:appendix1"));
XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
.render(data);
FileOutputStream out;
out = new FileOutputStream("F:/template2.docx");
template.write(out);
out.flush();
out.close();
template.close();
效果
所见即所得,标签的样式会应用到替换后的文本上,也可以通过代码设定文本的样式。
{
"text": "Sayi",
"style": {
"strike": false,
"bold": true,
"italic": false,
"color": "00FF00",
"underLine": false,
"fontFamily": "微软雅黑",
"fontSize": 12,
"highlightColor": "green",
"vertAlign": "superscript"
}
}
删除线
粗体
斜体
颜色
下划线
字体
字号
背景高亮色
上标或者下标
2.2.图片
代码
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.HyperLinkTextRenderData;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.util.BytePictureUtils;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Map<String, Object> data = new HashMap<>();
// 本地图片
data.put("local", new PictureRenderData(80, 100, "F:/图片.jpg"));
// 图片流
// data.put("localbyte", new PictureRenderData(80, 100, ".png", new FileInputStream("./logo.png")));
// 网络图片(注意网络耗时对系统可能的性能影响)
// data.put("urlpicture", new PictureRenderData(50, 50, ".png", BytePictureUtils.getUrlBufferedImage("http://deepoove.com/images/icecream.png")));
// java 图片
//data.put("bufferimage", new PictureRenderData(80, 100, ".png", bufferImage)));
XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
.render(data);
FileOutputStream out;
out = new FileOutputStream("F:/template2.docx");
template.write(out);
out.flush();
out.close();
template.close();
}
}
图片支持 BufferedImage,这意味着我们可以利用Java生成图表插入到word文档中。
{
"path": "",
"data": [],
"altMeta": "图片不存在",
"width": 100,
"height": 100
}
图片路径
图片也可以是byte[]字节数组
当无法获取图片时展示的文字
宽度,单位是像素
高度,单位是像素
2.3.表格
poi-tl默认实现了N行N列的样式(如下图),同时提供了当数据为空时,展示一行空数据的文案(如下图中的No Data Descs),数据模型是 MiniTableRenderData 。
代码
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Map<String, Object> data = new HashMap<>();
RowRenderData header = RowRenderData.build(new TextRenderData("FF0000", "姓名"), new TextRenderData("FF0000", "学历"));
RowRenderData row0 = RowRenderData.build("张三", "研究生");
RowRenderData row1 = RowRenderData.build("李四", "博士");
data.put("table", new MiniTableRenderData(header, Arrays.asList(row0, row1)));
XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
.render(data);
FileOutputStream out;
out = new FileOutputStream("F:/template2.docx");
template.write(out);
out.flush();
out.close();
template.close();
}
}
效果展示
MiniTableRenderData 的结构体
{
"rows": [
{
"cells": [
{
"cellText": [TextRenderData],
"cellStyle": {
"align": "center",
"backgroundColor": "ff9800"
}
}
],
"rowStyle": {
"align": "center",
"backgroundColor": "ff9800"
}
}
],
"header": {
"cells": [
{
"cellText": [TextRenderData],
"cellStyle": {
"align": "center",
"backgroundColor": "ff9800"
}
}
],
"rowStyle": {
"align": "center",
"backgroundColor": "ff9800"
}
},
"noDatadesc": "No Data Desc",
"style": {
"align": "center"
}
"width": 14.65
}
定义表格行数据
定义单元格数据,数据由 TextRenderData 指定
单元格样式:对齐方式,背景色
行样式:行数据的对齐方式,行背景色
定义表格头
没有数据的展示文案
表格样式:表格居左、居中、居右对齐
表格宽度(单位cm),表格的最大宽度 = 页面宽度 - 页边距宽度 * 2,页面宽度为A4(20.99 * 29.6,页边距为3.17 * 2.54)的文档最大表格宽度14.65CM。
2.4.列表
代码
package com.sunwayworld.sunrui;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Test {
public static void main(String[] args) throws IOException {
Map<String, Object> data = new HashMap<>();
data.put("list", new NumbericRenderData(new ArrayList<TextRenderData>() {
{
add(new TextRenderData("这个是第一个列"));
add(new TextRenderData("第二列"));
add(new TextRenderData("这是第三列"));
}
}));
XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
.render(data);
FileOutputStream out;
out = new FileOutputStream("F:/template2.docx");
template.write(out);
out.flush();
out.close();
template.close();
}
}
2.4.动态表格
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import java.util.*;
public class Test {
public static void main(String[] args) throws Exception {
List<Goods> goods = new ArrayList<>();
Goods good = new Goods();
good.setName("墙纸");
good.setDesc("书房卧室");
goods.add(good);
goods.add(good);
goods.add(good);
goods.add(good);
List<Labor> labors = new ArrayList<>();
Labor labor = new Labor();
labor.setCategory("油漆工");
labor.setPeople(2);
labors.add(labor);
labors.add(labor);
labors.add(labor);
labors.add(labor);
HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy();
Configure config = Configure.newBuilder().bind("goods", hackLoopTableRenderPolicy)
.bind("labors", hackLoopTableRenderPolicy).build();
XWPFTemplate template = XWPFTemplate.compile("F:/模板.docx",config).render(new HashMap<String, Object>() {
{
put("goods", goods);
put("labors", labors);
}
});
template.writeToFile("F:/啦啦啦.docx");
}
static class PaymentHackData {
private List<Goods> goods;
private List<Labor> labors;
public List<Goods> getGoods() {
return goods;
}
public void setGoods(List<Goods> goods) {
this.goods = goods;
}
public List<Labor> getLabors() {
return labors;
}
public void setLabors(List<Labor> labors) {
this.labors = labors;
}
}
static class Goods {
private String name;
private String desc;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
static class Labor {
private String category;
private int people;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
}
}
效果展示
关键代码:
发表评论