Java版Word开发工具Aspose.Words功能解析:查找和替换Word文档中的文本 您所在的位置:网站首页 java替换word中的内容 Java版Word开发工具Aspose.Words功能解析:查找和替换Word文档中的文本

Java版Word开发工具Aspose.Words功能解析:查找和替换Word文档中的文本

2023-12-11 13:07| 来源: 网络整理| 查看: 265

MS Word提供了一种简单的方法来查找和替换文档中的文本。查找和替换文本的一种流行用例之一可能是在文档之间的敏感信息在各个实体之间共享之前,对其进行删除或替换。但是,手动过程可能需要您安装MS Word并分别更新每个文档。在这种情况下,这将非常方便且省时,尤其是当您在桌面或Web应用程序中集成了查找和替换功能时。

在本文中,我将演示如何使用Java以编程方式在Word(DOC / DOCX)文档中查找和替换文本(单词或短语)。分步指南和代码示例将介绍在Word文档中查找和替换文本的各种方案。点击这里下载最新版(技术交流761297826)icon-default.png?t=M7J4https://www.evget.com/product/4116/download因此,来看看如何在各种情况下使用Java查找和替换Word文档中的文本。

使用Java在Word DOC / DOCX中查找和替换文本根据Word DOC / DOCX中的正则表达式模式替换相似的单词在Word文档的页眉/页脚中查找和替换文本在Word DOC / DOCX中用元字符查找和替换文本

①使用Java查找和替换Word DOC / DOCX文件中的文本

让我们从解决一个简单的查找和替换场景开始,在该场景中,我们将在输入的Word文档中找到单词“ Sad”。以下是执行此操作的步骤。

创建Document类的实例,然后将Word文档的路径传递给它。使用Document.getRange.replace(String,String,FindReplaceOptions)方法查找和替换文本。使用Document.save(String)方法保存文档。

下面的代码示例演示如何使用Java在Word DOCX中查找和替换文本。

// Load a Word DOCX document Document doc = new Document("document.docx"); // Find and replace text in the document doc.getRange().replace("sad", "[replaced]", new FindReplaceOptions(FindReplaceDirection.FORWARD)); // Save the Word document doc.save("Find-And-Replace-Text.docx");

输入Word文档

以下是找到并替换单词“ sad”后的输出。

②使用Java查找和替换DOC / DOCX中的相似单词

还可以自定义API,以根据相似度查找和替换文本。例如,单词“ sad”,“ mad”和“ bad”遵循类似的模式,以“ ad”结尾。电子邮件ID是此类文本的另一个示例。在这种情况下,您可以定义正则表达式模式来查找和替换具有特定模式的所有文本出现。以下是实现此目的的步骤。

创建Document类的实例,然后将Word文档的路径传递给它。使用Pattern.compile()方法定义一个正则表达式模式,并将其传递给Document.getRange()。replace(模式模式,字符串替换,FindReplaceOptions选项)方法。使用Document.save(String)方法保存更新的文档。

以下代码示例显示了如何使用Java根据特定的模式查找和替换相似的单词。

// Load a Word DOCX document Document doc = new Document("document.docx"); // Find and replace similar words in the document FindReplaceOptions options = new FindReplaceOptions(); doc.getRange().replace(Pattern.compile("[B|S|M]ad"), "[replaced]", options); // Save the Word document doc.save("Find-And-Replace-Text.docx");

以下是更新相似单词后的Word文档的屏幕截图。

③替换Word文档的页眉/页脚中的文本

Aspose.Words还允许您仅在Word文档的页眉/页脚中查找和替换文本。以下是执行此操作的步骤。

创建Document类的实例,然后将Word文档的路径传递给它。使用Document.getFirstSection()。getHeadersFooters()方法获取文档的HeaderFooterCollection。在HeaderFooter对象中检索特定的页眉/页脚。使用HeaderFooter.getRange()。replace()方法来查找和替换文本。保存更新的Word文档。

下面的代码示例演示如何使用Java查找和替换Word文档的页眉/页脚中的文本。

// Load a Word DOCX document Document doc = new Document("document.docx"); // Access header footer collection HeaderFooterCollection headersFooters = doc.getFirstSection().getHeadersFooters(); HeaderFooter footer = headersFooters.get(HeaderFooterType.FOOTER_PRIMARY); // Set find and replace options FindReplaceOptions options = new FindReplaceOptions(); options.setMatchCase(false); options.setFindWholeWordsOnly(false); footer.getRange().replace("This is footer of the document.", "Copyright (C) 2020 by Aspose Pty Ltd.", options); // Save the Word document doc.save("Find-And-Replace-Text.docx");

以下屏幕快照显示了Word文档页脚中的更新文本。

④使用Java在DOCX中使用元字符查找和替换文本

在某些情况下,需要查找并替换分为多行或多段的短语。在这种情况下,您将必须注意段落,节或换行符。Java的Aspose.Words使您轻松地轻松处理此类情况变得简单。以下是可用于不同休息时间的元字符:

&p:段落中断&b:分节符&m:分页符&l:换行

下面的代码示例演示如何在Word文档中使用段落分隔符查找和替换文本。

// Load a Word DOCX document Document doc = new Document("document.docx"); // Set options FindReplaceOptions options = new FindReplaceOptions(); // Disable matching case and finding whole words only options.setMatchCase(false); options.setFindWholeWordsOnly(false); // Replace text with paragraph break doc.getRange().replace("First paragraph ends.&pSecond paragraph starts.", "[replaced]", options); // Save the Word document doc.save("Find-And-Replace-Text.docx");

以下是输出Word文档的屏幕截图。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有