引入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
具体实现
准备替换实体:
@Data
@Builder
public class ReplaceParam {
//占位符
private String placeholder;
//替换值
private String replaceValue;
//默认值
private String defaultValue;
//文字大小
private Integer size;
//字体样式
private String fontFamily;
//字体颜色
private String color;
//..........
}
具体实现:
public class POIWordUtil {
public static File replace( File sourceFile, File targetFile, List<ReplaceParam> replaceParams) {
//创建文档
XWPFDocument xwpfDocument = DocUtil.create(sourceFile);
try (FileOutputStream outputStream = new FileOutputStream(targetFile)) {
Map<String, ReplaceParam> replaceParamMap = replaceParams.stream().collect(Collectors.toMap(ReplaceParam::getPlaceholder, Function.identity()));
for (IBodyElement iBodyElement : xwpfDocument.getBodyElements()) {
//段落
if (iBodyElement instanceof XWPFParagraph) {
XWPFParagraph xwpfParagraph = (XWPFParagraph) iBodyElement;
replaceText(replaceParamMap, xwpfParagraph);
}
//表格
if (iBodyElement instanceof XWPFTable) {
//多层表格则需要一直嵌套
replaceTables(replaceParamMap,(XWPFTable)iBodyElement);
}
}
xwpfDocument.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
return targetFile;
}
/**
* 处理表格问题,可能嵌套表格
*/
private static void replaceTables(Map<String, ReplaceParam> map, XWPFTable xwpfTable) {
for (XWPFTableRow row : xwpfTable.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
List<IBodyElement> bodyElements = cell.getBodyElements();
for (IBodyElement bodyElement : bodyElements) {
//表格中存在段落则进行段落替换
if (bodyElement instanceof XWPFParagraph) {
replaceText(map, (XWPFParagraph) bodyElement);
}
if (bodyElement instanceof XWPFTable) {
replaceTables(map, (XWPFTable)bodyElement);
}
}
}
}
}
/**
* 文本替换--样式保留为原来的样式
*/
private static void replaceText(Map<String, ReplaceParam> replaceParamMap, XWPFParagraph xwpfParagraph) {
String text = xwpfParagraph.getParagraphText();
if (StrUtil.isNotBlank(text) && text.contains("${")) {
List<XWPFRun> runs = xwpfParagraph.getRuns();
for (XWPFRun run : runs) {
//替换文字和样式
changeValue(run, replaceParamMap);
}
}
}
/**
* 文字或者样式替换
*/
private static void changeValue(XWPFRun run, Map<String, ReplaceParam> replaceParamMap) {
Set<Map.Entry<String, ReplaceParam>> textSets = replaceParamMap.entrySet();
for (Map.Entry<String, ReplaceParam> textSet : textSets) {
//匹配模板与替换值 格式${key}
String key = textSet.getKey() ;
ReplaceParam replaceParam = textSet.getValue();
if (run.toString().contains(key)) {
//替换值
String replaceValue = replaceParam.getReplaceValue() == null ? replaceParam.getDefaultValue() : replaceParam.getReplaceValue();
String value = run.toString().replace(key, replaceValue);
run.setText(value,0);
if (replaceParam.getSize() != null){
run.setFontSize(replaceParam.getSize());
}
if (replaceParam.getColor() != null){
run.setColor(replaceParam.getColor());
}
if (replaceParam.getFontFamily() != null){
run.setFontFamily(replaceParam.getFontFamily());
}
//...........................
}
}
}
public static void main(String[] args) {
ReplaceParam param1 = ReplaceParam.builder().placeholder("${add1}").replaceValue("星期一").size(40).build();
ReplaceParam param2 = ReplaceParam.builder().placeholder("${add2}").replaceValue("星期二").build();
ReplaceParam param3 = ReplaceParam.builder().placeholder("${add3}").replaceValue("星期三").size(30).build();
ReplaceParam param4 = ReplaceParam.builder().placeholder("${add4}").replaceValue("星期四").size(22).build();
ReplaceParam param5 = ReplaceParam.builder().placeholder("${add5}").replaceValue("星期五").build();
ReplaceParam param6 = ReplaceParam.builder().placeholder("${add6}").replaceValue("星期六").build();
ReplaceParam param7 = ReplaceParam.builder().placeholder("${add7}").replaceValue("星期日").build();
List<ReplaceParam> list = new ArrayList<>();
list.add(param1);
list.add(param2);
list.add(param3);
list.add(param4);
list.add(param5);
list.add(param6);
list.add(param7);
File sourceFile = new File("C:\\Users\\a\\Desktop\\study\\测试替换文档.docx");
File targetFile = new File("C:\\Users\\a\\Desktop\\study\\替换文档.docx");
POIWordUtil.replace(sourceFile, targetFile, list);
}
}
原始模板:
替换后:
注意
如果出现${替换符}出现没在一起的情况,像这种:
解决办法:
把 ${替换符} 先在记事本上连续写出来,再粘贴到文档中
标签:word,String,list,replaceValue,POI,ReplaceParam,替换,replaceParam From: https://blog.csdn.net/m0_73363097/article/details/140233145