我想删除 docx 中的特定行(如果它包含特定单词,例如“killer”)。如何使用 poi xwpf 编写程序?如果我用空数据替换它,该行仍然存在。
实际上,我可以在 docx 文件中找到特定文本,以便我可以使用以下代码进行匹配并决定是否删除该特定行:
for (XWPFParagraph xwpfParagraph : xwpfParagraphs) {
List<XWPFRun> xwpfRuns = xwpfParagraph.getRuns();
for (XWPFRun xwpfRun : xwpfRuns) {
String xwpfRunText = xwpfRun.getText(xwpfRun
.getTextPosition());
System.out.println(xwpfRunText);
for (Map.Entry<String, String> entry : replcementMap
.entrySet()) {
if (xwpfRunText != null
&& xwpfRunText.contains(entry.getKey())) {
xwpfRunText = xwpfRunText.replaceAll(
entry.getKey() , entry.getValue());
}
}
xwpfRun.setText(xwpfRunText, 0);
}
}
最佳答案
我也遇到了同样的问题。我想替换文档文件中的文本。 这是一个很好的例子: http://moodygeeky.wordpress.com/ 只需导入 2 个类即可:
There are 2 classes:
ZipUtility – 处理文件的压缩和解压缩
SubstituteText – 用于替换 .docx 文件中的文本
并将这些字符串替换为您的偏好:
// Names of placeholders, starting and ending with % (to be updated
// accordingly)
String placeholder1 = "%name%";
String placeholder2 = "%text%";
// Values to replace placeholders (to be updated accordingly)
String var1 = escapeHTML("newName"); // %name%
String var2 = escapeHTML("newText"); // %text%
关于java - 我想使用 poi xwpf 删除 docx 中的特定行(如果它有特定单词),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26210338/