1. maven依赖
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>5.2.2version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>5.2.2version>
dependency>
<dependency>
<groupId>com.deepoovegroupId>
<artifactId>poi-tlartifactId>
<version>1.10.0version>
dependency>
3. 编码
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordUtils2 {
public static void main(String[] args) {
Map<String, Object> datas = new HashMap<>();
datas.put("name", "张三");
List<String> positions = new ArrayList<>();
positions.add("学生");
positions.add("博士");
datas.put("position", positions);
datas.put("sex", "男");
datas.put("eMail", "[email protected]");
datas.put("header", "我是");
datas.put("header2", "头部");
datas.put("footer", "我是");
datas.put("footer2", "尾部");
datas.put("jpeg1", new PictureRenderData(100, 100, "C:\\Users\\Administrator\\Pictures\\Camera Roll\\f5c101aea5b0ba28038e77858224b20.jpg"));
datas.put("jpeg2", new PictureRenderData(100, 100, "C:\\Users\\Administrator\\Pictures\\Camera Roll\\20220223164431.jpg"));
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Map<String, Object> map = new HashMap<>();
map.put("img", new PictureRenderData(50, 50, "C:\\Users\\Administrator\\Pictures\\Camera Roll\\f5c101aea5b0ba28038e77858224b20.jpg"));
list.add(map);
}
datas.put("jpeg3", list);
List<Map<String, Object>> lists = new ArrayList<>();
for (int i = 0; i < 2; i++) {
Map<String, Object> map = new HashMap<>();
if (i == 0) {
map.put("name", "小金");
map.put("idNo", "362227");
List<Map<String,Object>> list1 = new ArrayList<>();
for (int j = 0; j < 3; j++) {
Map<String, Object> map1 = new HashMap<>();
map1.put("img", new PictureRenderData(50, 50, "C:\\Users\\Administrator\\Pictures\\Camera Roll\\f5c101aea5b0ba28038e77858224b20.jpg"));
list1.add(map1);
}
map.put("imgs",list1);
map.put("identity", "屌丝");
} else {
map.put("name", "小郭");
map.put("idNo", "362228");
List<Map<String,Object>> list1 = new ArrayList<>();
for (int j = 0; j < 3; j++) {
Map<String, Object> map1 = new HashMap<>();
map1.put("img", new PictureRenderData(50, 50, "C:\\Users\\Administrator\\Pictures\\Camera Roll\\20220223164431.jpg"));
list1.add(map1);
}
map.put("imgs",list1);
map.put("identity", "渣女");
}
lists.add(map);
}
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
Configure config = Configure.builder()
.bind("lists", policy)
.build(); // 设置列表配置 bind绑定需要渲染的list,有多少个绑定多少个
datas.put("lists", lists);
// 创建XWPFTemplate对象,并设置读取模板路径和要渲染的数据
XWPFTemplate template = XWPFTemplate.compile("D:\\test.docx", config).render(datas);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("D:\\word.docx");
template.write(outputStream);
outputStream.flush();
outputStream.close();
template.close();
} catch (Exception e) {
e.printStackTrace();
}
}
注:本文参照:https://blog.csdn.net/M625387195/article/details/124855854?spm=1001.2014.3001.5502