pom:
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.8.2</version>
</dependency>
自我介绍.doc
{{?item}}
{{name}} {{age}} {{@weChatPicture}} {{@picture}}
{{/item}}
main:
public static void main(String[] args) throws IOException {
Map<String, Object> map = new HashMap<>();
map.put("name", "tony");
map.put("age", "18");
// 读取本地磁盘图片
map.put("weChatPicture", new PictureRenderData(100, 100, "e:\\1.jpg"));
// 通过url读取网络图片
map.put("picture", new PictureRenderData(200, 400, ".png", BytePictureUtils.getUrlByteArray("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png")));
List<Map<String, Object>> list1 = new ArrayList<>();
list1.add(map);
list1.add(map);
Map<String, List<Map<String, Object>>> datasMap = new HashMap<>();
datasMap.put("item",list1);
File file = new File("D:\\自我介绍.docx");
XWPFTemplate template = XWPFTemplate.compile(file).render(datasMap);
FileOutputStream out = new FileOutputStream(new File("D:\\输出自我介绍.docx"));
template.write(out);
out.flush();
out.close();
template.close();
}