Developer หลายๆ คนใช้ Microsoft Office Interop library ในการสร้างเอกสาร Word แต่เรามักจะเห็นหลายๆ คนสอบถามมาว่าจะสร้างเอกสาร Word ในภาษา C++ โดยใช้โค้ดที่ง่ายแสนง่ายได้อย่างไร คุณสามารถสร้าง DOCX ได้ง่ายๆ โดยใช้ C++ และไม่ต้องทำงานร่วมกันหรือระบบอัตโนมัติของ Microsoft Office โดยใช้ขั้นตอนง่ายๆ
ขั้นตอนในการสร้างเอกสาร Word ด้วย C++
- ใช้แพ็คเกจ Aspose.Words.Cpp NuGet
- รวมการอ้างอิงถึง Aspose::Words และการบันทึกเนมสเปซ
- สร้างอินสแตนซ์ Document Class ใหม่
- สร้างอินสแตนซ์ DocumentBuilder Class วัตถุเพื่อทำงานกับไฟล์ Word
- ตั้งค่าข้อความและคุณสมบัติข้อความโดยใช้วัตถุ DocumentBuilder
- บันทึกเอกสาร Word เอาต์พุตบนดิสก์
ในตัวอย่างต่อไปนี้ เราจะเห็นวิธีสร้าง DOCX โดยทางโปรแกรมโดยใช้ C++ เราได้แสดงตัวอย่างสถานการณ์ง่ายๆ โดยเราได้เพิ่มข้อความสองสามบรรทัดและใช้การจัดรูปแบบตัวหนากับเอกสาร ขั้นตอนเหล่านี้ช่วยให้คุณเข้าใจวิธีดำเนินการอื่นๆ กับเอกสาร Word ที่คุณสามารถทำได้โดยใช้ Microsoft Interop และ MS Word โดยไม่ต้องพึ่งพาทั้งสองอย่าง
รหัสเพื่อสร้างเอกสาร Word โดยใช้ C ++
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Saving/DocSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <system/io/file.h> | |
#include <Aspose.Words.Cpp/License.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
using namespace Aspose::Words::Saving; | |
class AddingWordDocumentinCPP | |
{ | |
public: | |
void CreateWordDocumentinCPP() | |
{ | |
// File name and path of license file | |
System::String testLicenseFileName = u"Aspose.Total.NET.lic"; | |
// Setting the Aspose.Words before creating Word document | |
SharedPtr<License> wordsLicense = System::MakeObject<License>(); | |
// Setting license | |
wordsLicense->SetLicense(testLicenseFileName); | |
// Create an instance of Document class of Aspose.Words for C++ | |
// to add a blank Word document | |
SharedPtr<Document> WordDocumentUsingCPP = MakeObject<Document>(); | |
// Instantiate DocumentBuilder class to add content to the Word Document | |
SharedPtr<DocumentBuilder> WordDocumentBuilder = MakeObject<DocumentBuilder>(WordDocumentUsingCPP); | |
// Add some text in word file with formatting | |
WordDocumentBuilder->set_Bold(true); | |
WordDocumentBuilder->Writeln(u"We're adding this line of text in the word document using DocumentBuilder Class"); | |
WordDocumentBuilder->Writeln(u"This does not require Office Interop or Office Automation"); | |
// Save the generated word document to docx format | |
WordDocumentUsingCPP->Save(u"Word_Document_Created_using_CPP.docx"); | |
} | |
}; |
ก่อนหน้านี้ เราตรวจสอบ วิธีสร้างเอกสาร Word ใน C# โดยไม่ต้อง Interop ตอนนี้ เราได้เรียนรู้วิธีสร้าง DOCX โดยใช้ C++ ในการใช้ตัวอย่างข้างต้น คุณไม่จำเป็นต้องติดตั้ง Microsoft Word บนคอมพิวเตอร์หรือเซิร์ฟเวอร์ที่จะติดตั้งแอปพลิเคชัน สิ่งนี้ทำให้การสร้างเอกสารคำง่ายขึ้นมาก เร็วขึ้น และไม่ต้องพึ่งพาแพลตฟอร์มของ Microsoft