当前位置:网站首页>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 restart status of the openstack instance will change to the error handling method. The openstack built by the container restarts the compute service method of the computing node and prompts the gi
- openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法
- Altium Designer 19 器件丝印标号位置批量统一摆放
- 多线程基础部分Part2
- 汇编语言-王爽 第13章 int指令-笔记
- 线程间等待与唤醒机制、单例模式、阻塞队列、定时器
- JVM常用指令
- Sqlsever 字段相乘后保留2位小数
- Free SSH and telnet client putty
- Double position relay jdp-1440/dc110v
猜你喜欢
Dev++ environment setting C language keyword display color
Multithreading basic part part 1
使用 WordPress快速个人建站指南
Quick personal site building guide using WordPress
函数栈帧的形成与释放
Thinking technology: how to solve the dilemma in work and life?
cpu-z中如何查看内存的频率和内存插槽的个数?
JVM common instructions
Win 10 如何打开环境变量窗口
Junda technology - centralized monitoring scheme for multi brand precision air conditioners
随机推荐
Software testing year end summary report template
Two position relay hjws-9440
Using domain name forwarding mqtt protocol, pit avoidance Guide
What's new in redis4.0 - active memory defragmentation
310. minimum height tree
[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design
信息系统项目管理师---第七章 项目成本管理
1317. 将整数转换为两个无零整数的和
openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法
C language implementation timer
Sqlsever 字段相乘后保留2位小数
LeetCode 0086.分隔链表
Altium Designer 19 器件丝印标号位置批量统一摆放
[FPGA] design and implementation of frequency division and doubling based on FPGA
Netease cloud music params and encseckey parameter generation code
How to check the frequency of memory and the number of memory slots in CPU-Z?
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
JVM common instructions
QListWidget中的内容不显示
Program ape learning Tiktok short video production