public void findText(List<String> textList, List<String> labelList) {
if (null == textList || textList.size() == 0 || null == labelList || labelList.size() == 0) {
return;
}
// 目标标签
Set<String> labelSet = new HashSet<>(64);
String targetText, labelText;
int index, lastIndex, textLength;
boolean legal = true;
for (String text : textList) {
targetText = text;
while (StringUtils.isNoneBlank(targetText)
&& -1 != (index = targetText.indexOf("#{"))
&& -1 != (lastIndex = targetText.indexOf("}"))) {
// 避免出现类似于“}}}text#{”的情况出现
while (lastIndex < index) {
targetText = targetText.substring(lastIndex + 1);
index = targetText.indexOf("#{");
lastIndex = targetText.indexOf("}");
if (-1 == lastIndex) {
legal = false;
break;
}
}
if (!legal) {
break;
}
labelText = targetText.substring(index + 2, lastIndex);
// 解决“#{{{{#{labelName}}”读到 {{{#{labelName 的情况
while (-1 != (index = labelText.indexOf("#{")) && index < lastIndex) {
labelText = labelText.substring(index + 2);
}
labelSet.add(labelText.trim());
textLength = targetText.length();
if ((lastIndex + 1) < textLength) {
targetText = targetText.substring(lastIndex + 1);
} else {
break;
}
}
}
// 将从文本中读到的labelSet集合中的标签,筛选出符合labelList中的目标标签
}
public void replaceText(XWPFParagraph paragraph) {
if (null == paragraph) {
return;
}
List<XWPFRun> runList = paragraph.getRuns();
if (null == runList || 0 == runList.size()) {
return;
}
int length = runList.size();
for (int i = 0; i < length; i++) {
XWPFRun run = runList.get(i);
String targetText = run.getText(0);
if (StringUtils.isBlank(targetText)) {
continue;
}
if (targetText.contains("#") && !targetText.contains("}")) {
int endIndex = -1;
String tempText;
for (int j = i + 1; j < length; j++) {
tempText = runList.get(j).getText(0);
if (tempText.contains("}")) {
endIndex = j;
break;
}
}
if (-1 != endIndex) {
int tempIndex = targetText.lastIndexOf("#");
String prifix = targetText.substring(tempIndex, targetText.length());
targetText = targetText.substring(0, tempIndex);
StringBuilder finalText = new StringBuilder(prifix);
for (int k = i + 1; k <= endIndex; k++) {
String tText = runList.get(k).getText(0);
if (k == endIndex) {
int tIndex1 = tText.indexOf("}");
int tIndex2 = tText.indexOf("#");
if (tIndex1 < tIndex2 && k + 2 <= length) {
String lastTempText1 = tText.substring(0, tIndex1 + 1);
String lastTempText2 = tText.substring(tIndex1 + 1, tText.length());
runList.get(k + 1).setText(lastTempText2 + runList.get(k + 1).getText(0), 0);
finalText.append(lastTempText1);
} else {
finalText.append(tText);
}
} else {
finalText.append(tText);
}
if (k == endIndex) {
runList.get(k).setText(finalText.toString(), 0);
} else {
runList.get(k).setText("", 0);
}
}
}
} else if (targetText.contains("#{") && targetText.contains("}") && i + 2 <= length) {
int index = targetText.indexOf("}");
String suffix = targetText.substring(index + 1);
targetText = targetText.substring(0, index + 1);
XWPFRun secondRun = runList.get(i + 1);
secondRun.setText(suffix + secondRun.getText(0), 0);
}
// 标签替换
// 可以做一个枚举,一个判断是生成样式还是只做文字替换。如果是文件替换,可以直接使用targetText.replace
// 如果是生成样式,则使用POI
}
}
XmlCursor cursor = paragraph.getCTP().newCursor();
XWPFTable tableOne = doc2.insertNewTbl(cursor); // 指定坐标插入表格