翻譯|使用教程|編輯:胡濤|2022-12-05 10:38:14.770|閱讀 184 次
概述:在本文中,您將學(xué)習(xí)如何在 C++ 應(yīng)用程序中查找和替換 Word 文檔中的文本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Words 是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。此外,Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
查找和替換是 MS Word 提供的一項有用功能,可讓您一次更新所有出現(xiàn)的特定文本。因此,您不必手動定位和替換整個文檔中的文本。在本文中,您將學(xué)習(xí)如何在 C++ 應(yīng)用程序中查找和替換 Word 文檔中的文本。當(dāng)您需要一次替換多個文檔中的特定文本時,這可能很有用。
Aspose.Words for C++是一個用于創(chuàng)建新的和操作現(xiàn)有 Word 文檔的 C++ 庫。API 支持廣泛的基本和高級 Word 自動化功能。您可以下載API 包或使用NuGet安裝它。
Install-Package Aspose.Words.Cpp
以下是使用 Aspose.Words for C++ 在 Word 文檔中查找和替換文本的步驟。
以下代碼示例展示了如何使用 C++ 在 Word 文檔中查找單詞“sad”并將其替換為“bad”。
// Load MS Word document System::SharedPtr<Document> doc = System::MakeObject<Document>(u"Document.doc"); // Find and replace the text doc->get_Range()->Replace(u"sad", u"bad", System::MakeObject<FindReplaceOptions>(FindReplaceDirection::Forward)); // Save the updated document doc->Save(u"updated.doc");
您還可以定義正則表達式以查找和替換符合特定模式的單詞。例如,您可以將“sad”和“mad”替換為“bad”。以下是在 Word 文檔中查找和替換與正則表達式匹配的單詞的步驟。
以下代碼示例展示了如何在 C++ 中使用正則表達式查找和替換文本。
// Load MS Word document System::SharedPtr<Document> doc = System::MakeObject<Document>(u"Document.doc"); // Create find and replace options System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>(); // Find and replace the text doc->get_Range()->Replace(System::MakeObject<System::Text::RegularExpressions::Regex>(u"[s|m]ad"), u"bad", options); // Save the updated document doc->Save(u"updated.doc");
在某些情況下,您要替換的文本可能包括換行符、分段符、分節(jié)符等。為了處理這種情況,Aspose.Words for C++ 在搜索和替換字符串中支持以下元字符.
以下代碼示例顯示如何在 Word 文檔中使用元字符查找和替換文本。
// Load MS Word document System::SharedPtr<Document> doc = System::MakeObject<Document>(u"Document.doc"); // Create find and replace options System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>(); // Find and replace the text doc->get_Range()->Replace(u"This is Line 1&pThis is Line 2", u"This is replaced line", options); doc->get_Range()->Replace(u"This is Line 1&mThis is Line 2", u"Page break is replaced with new text.", options); // Save the updated document doc->Save(u"updated.doc");
Aspose.Words for C++ 還允許您在查找和替換操作期間忽略字段中的文本和修訂。FindReplaceOptions類允許您指定添加此自定義的選項FindReplaceOptions 類提供了以下方法來在不同場景下忽略文本:
以下代碼示例顯示了如何在上述每種情況下忽略文本。
// Load MS Word document System::SharedPtr<Document> doc = System::MakeObject<Document>(u"Document.doc"); // Create find and replace options System::SharedPtr<FindReplaceOptions> options = System::MakeObject<FindReplaceOptions>(); // Replace 'e' in document ignoring text inside field options->set_IgnoreFields(true); doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options); // Replace 'e' in document ignoring deleted text options->set_IgnoreDeleted(true); doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options); // Replace 'e' in document ignoring inserted text options->set_IgnoreInserted(true); doc->get_Range()->Replace(System::MakeObject<Regex>(u"e"), u"*", options); // Save the updated document doc->Save(u"updated.doc");
以上便是使用 C++ 在 Word 文檔中查找和替換文本詳細步驟 ,要是您還有其他關(guān)于產(chǎn)品方面的問題,歡迎咨詢我們,或者加入我們官方技術(shù)交流群。
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn