WordExportUtil.changeTableMessage()
这种方式本质为删除再新增,无法保持原有样式,如果需要保持样式,请看我另一篇。
注意事项,1.cell.getText(),自己写的时候cell可能出点不出.getText方法,不知道为什么,但是好用。。。2.替换文本的占位符为 ${xxx} ,在word中填写的时候,需要一次性输入,最好是在text编辑器中写好,然后复制过去。否则可能会被识别为 ${,xxx,} ,就无法替换了。
package org.jeecg.modules.utils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Slf4j
public class WordExportUtil {
/**
* @param inputStream 模板文件流
* @param textMap 待写入字符map
* @return 新文件流
*/
public static InputStream changWord(InputStream inputStream, Map<String, String> textMap) {
try {
//读取模板信息,替换指定位置字符
XWPFDocument document = new XWPFDocument(inputStream);
if (!WordExportUtil.changeText(document, textMap)) {
return null;
}
//新文件(XWPFDocument)转成文件流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.write(byteArrayOutputStream);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return byteArrayInputStream;
} catch (Exception e) {
log.warn(“word写入数据异常: “+ JSONUtil.toJsonStr(textMap));
log.warn(e.getMessage());
}
return null;
}
/**
* 替换文本 *
* @param document docx解析对象
* @param valueMap 待写入字符map
*/
public static Boolean changeText(XWPFDocument document, Map<String, String> valueMap) {
List<XWPFParagraph> paragraphs = document.getParagraphs();
if (CollectionUtil.isEmpty(paragraphs)) {
return false;
}
for (XWPFParagraph paragraph : paragraphs) {
String text = paragraph.getText();
if (StringUtils.isBlank(text)) {
continue;
}
if (text.indexOf(“$”) == -1) {
continue;
}
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
String value = setWordValue(run.toString(), valueMap);
run.setText(value, 0);
}
}
List<XWPFTable> tables = document.getTables();
if (CollectionUtil.isEmpty(tables)) {
return false;
}
for (XWPFTable table : tables) {
String text = table.getText();
if (StringUtils.isBlank(text)) {
continue;
}
if (text.indexOf(“$”) == -1) {
continue;
}
//String value = setWordValue(table.getText(), valueMap);
changeTableMessage(valueMap,table,false,null);
}
return true;
}
/**
* word中的表格文字替换
*/
public static void changeTableMessage(Map<String, String> params, XWPFTable table, boolean isBold, Integer fontSize) {
int count = table.getNumberOfRows();//获取table的行数
for (int i = 0; i < count; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {//遍历每行的值并进行替换
System.out.println(cell.getText());
for (Map.Entry<String, String> e : params.entrySet()) {
if (cell.getText().equals(“${” + e.getKey() + “}”)) {
XWPFParagraph newPara = new XWPFParagraph(cell.getCTTc().addNewP(), cell);
XWPFRun r1 = newPara.createRun();
r1.setBold(isBold);
if (fontSize != null) {
r1.setFontSize(fontSize);
}
r1.setText(e.getValue());
cell.removeParagraph(0);
cell.setParagraph(newPara);
}
}
}
}
}
/**
* 匹配传入信息集合与模板
* @param value 模板需要替换的区域
* @param textMap 传入信息集合
* @return 模板需要替换区域信息集合对应值
*/
public static String setWordValue(String value, Map<String, String> textMap) {
Set<Map.Entry<String, String>> textSets = textMap.entrySet();
for (Map.Entry<String, String> textSet : textSets) {
String key = “${” + textSet.getKey() + “}”;
if (value.indexOf(key) != -1) {
value = textSet.getValue();
}
}
return value;
}
}
————————————————
版权声明:本文为CSDN博主「不偏不易94」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yyfjm1994/article/details/118514585