当前位置:网站首页>POI 替换docx中的文字和图片
POI 替换docx中的文字和图片
2022-06-27 06:03:00 【YUELEI118】
参考文档:链接
1. 修改docx文件,给要替换的内容命名
替换为文字:{ {name}}
替换为图片:{ {@image}}
D:/ old.docx
2. 代码部分
步骤:
- 创建map集合将要替换的内容写入
- 处理图片
- 替换docx文件
- 写到硬盘中
public class ReplaceDocx {
// 替换docx文件中的信息
private File docxReplace(String wordPath) {
// 将要替换的文字、图片写入map集合中
Map<String, Object> map = new HashMap<>();
map.put("name", "张三");
map.put("sex", "男");
map.put("address", "北京市XX区XX路");
try {
// 图片的处理
PictureRenderData pictureRenderData = Pictures.ofStream(new FileInputStream("d:\\zhangsan.jpg"), PictureType.PNG)
.size(102, 126).create();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
map.put("photo", pictureRenderData);
// 生成的新文件保存路径
File repotrFile = new File("D:\\new.docx");
try {
// 生成新的文件并写到硬盘
XWPFTemplate.compile(wordPath).render(map, new FileOutputStream(repotrFile));
} catch (IOException e) {
e.printStackTrace();
}
// Todo
// 可以对文件执行其他操作,例如:打印,在打印完毕后删除文件
// repotrFile.delete();
// 或者直接返回File
return repotrFile;
}
public static void main(String[] args) {
File file = new ReplaceDocx().docxReplace("D:\\old.docx");
}
}
3. 重点
- map集合中定义的类型:
Map<String, Object>如果有图片则需要将vlaue值定义为Object,如果替换的内容全部是字符,则只需要定义为Map<String, String>
1.1 注意map集合key的值,只需要{ {}}内的字符串即可,其他教程使用的方法,可能需要带符号。 - 图片的处理:
Pictures.ofStream(new FileInputStream("d:\\zhangsan.jpg"), PictureType.PNG).size(102, 126).create()
2.1 Pictures的构造方法有很多,此例是通过字符流构造,还可以通过BufferedImage、base64、byte[]等参数构造,详见源码。
2.2 第二个参数是创建的图片格式
2.3 size()方法,很显然是指定生成的图片大小
2.4 最重要的一点:在docx模板文件中图片的名字前要加@ - 替换模板文件并写到硬盘:
XWPFTemplate.compile(wordPath).render(map, new FileOutputStream(repotrFile));
边栏推荐
- The form verifies the variables bound to the V-model, and the solution to invalid verification
- Junda technology - centralized monitoring scheme for multi brand precision air conditioners
- openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法
- 【Cocos Creator 3.5.1】event. Use of getbutton()
- Thinking technology: how to solve the dilemma in work and life?
- 项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
- IP网络通信的单播、组播和广播
- JVM类加载机制
- Dev++ 环境设置C语言关键字显示颜色
- 信息系统项目管理师---第七章 项目成本管理
猜你喜欢

思维的技术:如何破解工作生活中的两难冲突?

JVM类加载机制

Kubesphere cluster configuration NFS storage solution - favorite

427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)

Asp. Net core6 websocket simple case

Altium designer 19 device silk screen label position shall be placed uniformly in batches

openresty使用文档

我对于测试团队建设的意见

Leetcode99 week race record

Nlp-d62-nlp competition d31 & question brushing D15
随机推荐
IAR systems fully supports Centrino technology 9 series chips
JVM类加载机制
函数式 连续式
[FPGA] UART serial port_ V1.1
Proxy reflect usage details
【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
C language implementation timer
[cocos creator 3.5.1] addition of coordinates
JVM tuning ideas
爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
Leetcode298 weekly race record
JVM的垃圾回收机制
Two position relay hjws-9440
代码即数据
【Cocos Creator 3.5.1】event.getButton()的使用
Double position relay jdp-1440/dc110v
Openresty usage document
Two position relay rxmvb2 r251 204 110dc
【Cocos Creator 3.5.1】input. Use of on