当前位置:网站首页>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));
边栏推荐
- Assembly language - Wang Shuang Chapter 3 notes and experiments
- Spark 之 built-in functions
- Dev++ environment setting C language keyword display color
- Openresty usage document
- Wechat applet refreshes the current page
- JVM类加载机制
- Logu p4683 [ioi2008] type printer problem solving
- 开门小例子学习十种用例图
- IAR systems fully supports Centrino technology 9 series chips
- mysql 查询时将状态改为相对应的文字
猜你喜欢

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

免费的 SSH 和 Telnet 客户端PuTTY
![[FPGA] design and implementation of frequency division and doubling based on FPGA](/img/84/75d473d3d8e670260ba16d06705c2f.png)
[FPGA] design and implementation of frequency division and doubling based on FPGA

Quick personal site building guide using WordPress

汇编语言-王爽 第8章 数据处理的两个基本问题-笔记

JVM garbage collection mechanism

Dev++ environment setting C language keyword display color

IAR Systems全面支持芯驰科技9系列芯片

Proxy-Reflect使用详解

Altium designer 19 device silk screen label position shall be placed uniformly in batches
随机推荐
我对于测试团队建设的意见
汇编语言-王爽 第11章 标志寄存器-笔记
Using CSDN to develop cloud and build navigation websites
Configuration of vscode korofileheader
【合辑】点云基础知识及点云催化剂软件功能介绍
LeetCode-515. Find the maximum value in each tree row
Proxy-Reflect使用详解
JVM调优思路
思维的技术:如何破解工作生活中的两难冲突?
JVM的垃圾回收机制
程序猿学习抖音短视频制作
The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
Download CUDA and cudnn
IAR systems fully supports Centrino technology 9 series chips
Navigation [machine learning]
693. 交替位二进制数
My opinion on test team construction
IAR Systems全面支持芯驰科技9系列芯片
JVM类加载机制
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