word的x中的文字图片替换说明.doc
文本预览下载声明
1,需要的jar:
2,替换docx文档的文字、图片实例
* 注意事项:
* word中的替换的文字replaceStr一定是从普通txt文本中粘贴过来的,这样才可以保证这些replaceStr的所有属性一致
* 才能保证这些replaceStr在同一个XWPFRun中。
public class WordTest2 {
String templatePathX = D:/temp/打印样本/poi/打印样本.docx;
String savePath = D:/temp/打印样本/poi/打印样本-temp.docx;
//image
String logoImagePath = D:/temp/打印样本/forest_logo.png;
String IconImagePath = D:/temp/打印样本/icon.jpg;
@Test
public void testDocx() {
try {
WordDocxUtil wordDocxUtil=new WordDocxUtil(templatePathX);
// ArrayListString replaceEles =wordDocxUtil.getReplaceElements(\\$\\{[^{}]+\\});
// for(String replaceEle:replaceEles){
// System.out.println(replaceEle);
// }
//替换文字
// MapString, String map = new HashMapString, String();
// map.put(${orderId}, B123123);
// map.put(${boxName}, 王五);
// map.put(${course-1}, 语文);
// map.put(${score}, 85);
// wordDocxUtil.replaceText(map);
//单个文字替换
// wordDocxUtil.replaceText(${course}, 语文);
//正则表达式文字替换
// wordDocxUtil.replaceTextByRegex(\\$\\{course-\\d\\}, [-_-]);
//替换图片
// MapString, Object imageMap = new HashMapString, Object();
// imageMap.put(width, 50);
// imageMap.put(height, 50);
// imageMap.put(imageType, WordDocxUtil.getSuffix(logoImagePath));
// imageMap.put(content, WordDocxUtil.inputStream2ByteArray(new FileInputStream(logoImagePath), true));
// wordDocxUtil.replaceImage(${forestLogo}, imageMap);
//
// imageMap = new HashMapString, Object();
// imageMap.put(width, 50);
// imageMap.put(height, 50);
// imageMap.put(imageType, WordDocxUtil.getSuffix(IconImagePath));
// imageMap.put(content, WordDocxUtil.inputStream2ByteArray(new FileInputStream(IconImagePath), true));
// wordDocxUtil.replaceImage(${icon}, imageMap);
// setComboboxValue(paytype, 1,3,5,6);
// setComboboxValue(makeType, aa,vv);
wordDocxUtil.save(savePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2,导入docx文档的操作帮助类
package com
显示全部