يستخدم العديد من المطورين مكتبة Microsoft Office Interop لإنشاء مستندات 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# بدون إمكانية التشغيل المتداخل. الآن ، تعلمنا كيفية إنشاء DOCX باستخدام C ++. لاستخدام المثال أعلاه ، لا تحتاج إلى تثبيت Microsoft Word على جهاز الكمبيوتر الخاص بك أو الخادم حيث سيتم تثبيت التطبيق. هذا يجعل إنشاء مستند Word أسهل كثيرًا وأسرع ولا يعتمد على نظام Microsoft الأساسي.