当前位置:网站首页>POI replacing text and pictures in docx
POI replacing text and pictures in docx
2022-06-27 06:39:00 【YUELEI118】
Reference documents : link
1. modify docx file , Name the content you want to replace
Replace with text :{ {name}}
Replace with picture :{ {@image}}
D:/ old.docx
2. Code section
step :
- establish map Set writes the contents to be replaced to
- Processing images
- Replace docx file
- Write to hard disk
public class ReplaceDocx {
// Replace docx Information in the file
private File docxReplace(String wordPath) {
// Text to be replaced 、 Picture writing map Collection
Map<String, Object> map = new HashMap<>();
map.put("name", " Zhang San ");
map.put("sex", " male ");
map.put("address", " The Beijing municipal XX District XX road ");
try {
// Image processing
PictureRenderData pictureRenderData = Pictures.ofStream(new FileInputStream("d:\\zhangsan.jpg"), PictureType.PNG)
.size(102, 126).create();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
map.put("photo", pictureRenderData);
// The path to save the generated new file
File repotrFile = new File("D:\\new.docx");
try {
// Create a new file and write it to the hard disk
XWPFTemplate.compile(wordPath).render(map, new FileOutputStream(repotrFile));
} catch (IOException e) {
e.printStackTrace();
}
// Todo
// You can perform other operations on files , for example : Print , Delete the file after printing
// repotrFile.delete();
// Or just go back File
return repotrFile;
}
public static void main(String[] args) {
File file = new ReplaceDocx().docxReplace("D:\\old.docx");
}
}
3. a key
- map Types defined in the collection :
Map<String, Object>If there are pictures, you need to vlaue Value is defined as Object, If the replacement is all characters , Then it only needs to be defined asMap<String, String>
1.1 Be careful map aggregate key Value , It only needs { {}} String in the , Methods used in other tutorials , May need to be signed . - Image processing :
Pictures.ofStream(new FileInputStream("d:\\zhangsan.jpg"), PictureType.PNG).size(102, 126).create()
2.1 Pictures There are many ways to construct , This example is constructed by character stream , You can also use BufferedImage、base64、byte[] Isoparametric construction , See the source code for details .
2.2 The second parameter is the image format created
2.3 size() Method , Obviously, it is to specify the size of the generated image
2.4 The most important point : stay docx The name of the picture in the template file should be preceded by @ - Replace the template file and write to the hard disk :
XWPFTemplate.compile(wordPath).render(map, new FileOutputStream(repotrFile));
边栏推荐
- 面试官:大量请求 Redis 不存在的数据,从而打倒数据库,你有什么方案?
- 第 299 场周赛 第四题 6103. 从树中删除边的最小分数
- HTAP 快速上手指南
- tracepoint
- 建模竞赛-光传送网建模与价值评估
- Proxy reflect usage details
- [QT] use structure data to generate read / write configuration file code
- Matlab GUI interface simulation DC motor and AC motor speed simulation
- TiDB与 MySQL 兼容性对比
- Force buckle 179, max
猜你喜欢

Block level elements & inline elements

Redis cache penetration, cache breakdown, cache avalanche

Distribution gaussienne, régression linéaire, régression logistique

matlab GUI界面仿真直流电机和交流电机转速仿真

Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)

使用CSDN 开发云搭建导航网站

426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary

MPC control of aircraft wingtip acceleration and control surface

小米面试官:听你说精通注册中心,我们来聊 3 天 3 夜

IDEA中关于Postfix Completion代码模板的一些设置
随机推荐
论文阅读技巧
Redis cache penetration, cache breakdown, cache avalanche
LeetCode 0086.分隔链表
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
Optimistic and pessimistic affairs
Overview of database schema in tidb
IDEA中关于Postfix Completion代码模板的一些设置
Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes
JVM common instructions
TiDB 中的视图功能
extendible hashing
Caldera installation and simple use
【LeetCode】Day90-二叉搜索树中第K小的元素
Transaction overview of tidb
MPC control of aircraft wingtip acceleration and control surface
小米面试官:听你说精通注册中心,我们来聊 3 天 3 夜
thrift
Unrecognized VM option ‘‘
Partial function of Scala
TiDB 中的SQL 基本操作