หัวข้อนี้แสดง วิธีค้นหาและแทนที่ข้อความในเอกสาร Word ใน C+* โดยใช้ตัวอย่างโค้ดแบบง่าย การใช้ C++ แทนที่ข้อความในเอกสาร Word ทำได้อย่างง่ายดายด้วยโค้ดง่าย ๆ ไม่กี่บรรทัด โดยไม่ต้องพึ่งพา MS Word หรือไลบรารี Interop จากภายนอก คุณสามารถใช้แอปพลิเคชันในแอปพลิเคชันที่รองรับ C++ ที่ทำงานบนแพลตฟอร์ม Windows, macOS หรือ Linux
ขั้นตอนการแทนที่ข้อความในเอกสาร Word ด้วย C++
- กำหนดค่า Aspose.Words.CPP เวอร์ชันล่าสุดจาก NuGet Package Manager
- รวมเนมสเปซ Aspose::Words และเพิ่มไฟล์ส่วนหัวที่จำเป็น
- เริ่มต้นวัตถุคลาส Document เพื่อโหลด DOCX เพื่อแทนที่ข้อความ
- สร้างวัตถุคลาส FindReplaceOptions เพื่อเปิดใช้งานคุณสมบัติต่างๆ
- ดำเนินการแทนที่ข้อความสำหรับการค้นหาที่เลือกและแทนที่สตริง
- บันทึกเอกสาร Word ด้วยข้อความที่ถูกแทนที่บนดิสก์
ในขั้นตอนข้างต้น ก่อนอื่นเราจะโหลดเอกสาร Word ต้นทางจากดิสก์ จากนั้นระบุคุณสมบัติต่างๆ เพื่อกำหนดเกณฑ์สำหรับการค้นหาและแทนที่ข้อความ รวมถึงทิศทางการแทนที่ ตัวพิมพ์ที่ตรงกัน และการค้นหาทั้งคำ สุดท้าย เราจะ *แทนที่ข้อความในเอกสารเวิร์ดโดยใช้ C++ และบันทึก DOCX ที่แก้ไขแล้วลงในดิสก์
รหัสเพื่อแทนที่ข้อความในเอกสาร Word โดยใช้ C ++
#pragma once | |
#include <cstdint> | |
#include <iostream> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/Range.h> | |
#include <Aspose.Words.Cpp/Replacing/FindReplaceOptions.h> | |
#include <Aspose.Words.Cpp/Replacing/FindReplaceDirection.h> | |
#include <system/enumerator_adapter.h> | |
#include <system/io/path.h> | |
#include <system/smart_ptr.h> | |
#include <system/shared_ptr.h> | |
using namespace Aspose::Words; | |
using namespace Aspose::Words::Replacing; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
class FindAndReplaceTextEx | |
{ | |
public: | |
static void FindAndReplaceText() | |
{ | |
// Load and Set API License | |
System::String LicFilePath = u"Aspose.Total.Net.lic"; | |
SharedPtr<Aspose::Words::License> WordsCPPLicenseForTable = System::MakeObject<Aspose::Words::License>(); | |
// Setting Aspose.Words product license | |
WordsCPPLicenseForTable->SetLicense(LicFilePath); | |
// Load an input Word file with Document class for replacing text | |
SharedPtr<Document> FindInDocument = MakeObject<Document>(u"Test.docx"); | |
// Instantiate FindReplaceOptions class object to replace text string | |
SharedPtr<FindReplaceOptions> options = MakeObject< FindReplaceOptions>(); | |
options->set_MatchCase(true); | |
options->set_Direction(FindReplaceDirection::Forward); | |
options->set_FindWholeWordsOnly(true); | |
// Apply search and replace string in the Replace method | |
FindInDocument->get_Range()->Replace(u"Test", u"Replace", options); | |
// Save the document on disk with replaced text | |
FindInDocument->Save(u"FindAndReplace.docx"); | |
} | |
}; |
ในตัวอย่างนี้ การใช้ C++ เอกสาร Word แทนที่ข้อความ ทำได้ง่ายๆ โดยใช้การเรียก API อย่างง่าย เมื่อใช้อินสแตนซ์ของ Document class เอกสาร Word ที่ป้อนจะถูกโหลดโดยใช้ API จากนั้นในขั้นตอนต่อๆ ไป เราจะใช้คลาส FindReplaceOptions เพื่อตั้งค่าเกณฑ์การค้นหาและแทนที่ด้วยการตั้งค่าคุณสมบัติต่างๆ สุดท้าย เราจะบันทึก DOCX ที่แก้ไขบนดิสก์หรือใน MemoryStream เป็นเอาต์พุต
ในบทความนี้ เราได้สำรวจเกี่ยวกับวิธีการ *ค้นหาและแทนที่ข้อความในเอกสาร Word โดยใช้ C++ อย่างไรก็ตาม หากคุณต้องการเรียนรู้เกี่ยวกับการส่งออกไฟล์ Word เป็น PDF โปรดดูบทความใน วิธีแปลง Word เป็น PDF โดยใช้ C ++