このトピックでは、C++を使用してDOCXでテーブルを作成する方法を説明します。テーブルはWord文書内で一般的に使用され、C++を使用してDOCXにテーブルを挿入できます。単純なAPI呼び出しを使用して、WordテーブルをC++に挿入できます。
C++を使用してDOCXにテーブルを追加する手順
- NuGetから最新バージョンのAspose.Words.Cppをインストールします
- Aspose::Words名前空間への参照を含める
- Document Classオブジェクトを作成して、テーブルを追加するためのDOCXをロードします
- DocumentBuilder Classをインスタンス化して、DOCX内のコンテンツを管理します
- DocumentBuilderを使用して、セル内に行、セル、画像を追加します
- Saveメソッドを使用してC++でテーブル付きのDOCXを保存します
以下の例では、MicrosoftWordに依存せずにC++を使用してWordテーブルを作成できます。テーブルセル内のコンテンツをフォーマットでき、セル内に画像を追加する方法も示しました。
C++を使用してDOCXにテーブルを追加するコード
This file contains 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
#pragma once | |
#include <cstdint> | |
#include <iostream> | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBase.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Drawing/Shape.h> | |
#include <Aspose.Words.Cpp/HeaderFooterType.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <system/array.h> | |
#include <system/exceptions.h> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <system/io/path.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
class CreateTableInWordDocumentUsingCpp | |
{ | |
public: | |
void AddTableinWordDocument() | |
{ | |
// Set License file name | |
System::String LicenseFile = u"Aspose.Total.NET.lic"; | |
// Setting the Aspose.Words before creating Word document | |
SharedPtr<License> wordsLicenseForTable = System::MakeObject<License>(); | |
// Setting product license | |
wordsLicenseForTable->SetLicense(LicenseFile); | |
// Instantiate Document class to load DOCX and add table | |
SharedPtr<Document> AddTableToWordDOC = MakeObject<Document>(u"WordDocument.docx"); | |
// Instantiate DocumentBuilder class to manage document content | |
SharedPtr<DocumentBuilder> TableWriter = MakeObject<DocumentBuilder>(AddTableToWordDOC); | |
// Mark the start of table | |
TableWriter->StartTable(); | |
// Insert Row and first Cell inside Word Table | |
TableWriter->InsertCell(); | |
// Add some text in Table Cell | |
TableWriter->Write(u"Table Row 1 and Cell 1"); | |
// Add a new Cell inside Row | |
TableWriter->InsertCell(); | |
// Insert an Image in Word Table Cell | |
TableWriter->InsertImage(u"image in table.jpg"); | |
// Mark end of Table Row | |
TableWriter->EndRow(); | |
// Mark end of Word Table creation | |
TableWriter->EndTable(); | |
// Save the word document with table to docx format | |
AddTableToWordDOC->Save(u"InsertTableinDocx.docx"); | |
} | |
}; |
以前、C++を使用してDOCXに画像を追加する方法を見ました。ただし、このトピックでは、C++を使用してDOCXにテーブルを挿入する方法を学びました。