在本主题中,我们将学习如何在 C++ 中创建 DOCX。 Word 文档以编程方式创建,尤其是在 C++ 中,并使用许多应用程序来实现自动化,例如生成水电费账单。您可以使用简单的 API 接口在 C++ 中动态创建 DOCX。
在 C++ 中创建 DOCX 的步骤
- 使用 NuGet 包管理器包含 Aspose.Words.Cpp
- 添加对 Aspose::Words 和 Aspose::Words::Saving 和 System::Drawing 命名空间的引用
- 创建 Document Class 的实例以添加空白 Word 文档
- 创建 DocumentBuilder Class 实例来处理 Word 文档
- 在文档中添加文本以及格式
- 使用 Save 方法以 DOCX 格式保存具有自定义格式的 Word 文档
在下面的示例中,我们展示了如何通过添加文本并设置其格式来在 C++ 中创建 word 文档。您还将看到如何超链接文档中的文本。
在 C++ 中创建 DOCX 的代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 <Aspose.Words.Cpp/Font.h> | |
#include <Aspose.Words.Cpp/Border.h> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <Aspose.Words.Cpp/Saving/CompressionLevel.h> | |
#include <Aspose.Words.Cpp/Saving/OoxmlCompliance.h> | |
#include <Aspose.Words.Cpp/Saving/OoxmlSaveOptions.h> | |
#include <system/io/file.h> | |
#include <drawing/color.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; | |
using namespace System::Drawing; | |
class CreatingWordDocumentinCPP | |
{ | |
public: | |
void CreateWordDocumentinCPP() | |
{ | |
// Access liense file and set for API | |
System::String WordsLicenseFileName = u"Aspose.Total.NET.lic"; | |
SharedPtr<License> wordsLic = System::MakeObject<License>(); | |
wordsLic->SetLicense(WordsLicenseFileName); | |
// Create Document Class instance to add a blank Word document | |
SharedPtr<Document> WordDocUsingCPP = MakeObject<Document>(); | |
// Create instance of DocumentBuilder class to process the Word Document | |
SharedPtr<DocumentBuilder> WordDocBuilder = MakeObject<DocumentBuilder>(WordDocUsingCPP); | |
// Insert a string surrounded by a border | |
WordDocBuilder->get_Font()->get_Border()->set_Color(Color::get_Green()); | |
WordDocBuilder->get_Font()->get_Border()->set_LineWidth(2.5); | |
WordDocBuilder->get_Font()->get_Border()->set_LineStyle(LineStyle::DashDotStroker); | |
WordDocBuilder->Write(u"Text with a green border around it."); | |
// Remove the font formatting and set it explicitly | |
WordDocBuilder->get_Font()->ClearFormatting(); | |
// Adding paragraph break for text | |
WordDocBuilder->InsertBreak(BreakType::ParagraphBreak); | |
WordDocBuilder->Write(u"For more info, please visit oue site"); | |
// Add a hyperlink and using custom formatting. The hyperlink shall | |
// be a clickable text which will redirect to URL set inside it | |
WordDocBuilder->get_Font()->set_Color(Color::get_Blue()); | |
WordDocBuilder->get_Font()->set_Underline(Underline::Single); | |
WordDocBuilder->InsertHyperlink(u"Aspose Knowledge Base", u"https://kb.aspose.com/", false); | |
WordDocBuilder->get_Font()->ClearFormatting(); | |
WordDocBuilder->Writeln(u"."); | |
SharedPtr <OoxmlSaveOptions> DocSaveOptions = MakeObject<OoxmlSaveOptions>(); | |
DocSaveOptions->set_Compliance(OoxmlCompliance::Iso29500_2008_Strict); | |
DocSaveOptions->set_SaveFormat(SaveFormat::Docx); | |
// Save the generated DOCX file using Save method | |
WordDocUsingCPP->Save(u"Word_Doc_Created_using_CPP.docx", DocSaveOptions); | |
} | |
}; |
在前面的示例中,我们查看了 如何使用 C++ 在 DOCX 中的表中添加行。本主题的重点是使用 C++ 从头开始生成 Word 文档。