C++を使用してDOCXでテーブルを作成する方法

このトピックでは、C++を使用してDOCXでテーブルを作成する方法を説明します。テーブルはWord文書内で一般的に使用され、C++を使用してDOCXにテーブルを挿入できます。単純なAPI呼び出しを使用して、WordテーブルをC++に挿入できます。

C++を使用してDOCXにテーブルを追加する手順

  1. NuGetから最新バージョンのAspose.Words.Cppをインストールします
  2. Aspose::Words名前空間への参照を含める
  3. Document Classオブジェクトを作成して、テーブルを追加するためのDOCXをロードします
  4. DocumentBuilder Classをインスタンス化して、DOCX内のコンテンツを管理します
  5. DocumentBuilderを使用して、セル内に行、セル、画像を追加します
  6. Saveメソッドを使用してC++でテーブル付きのDOCXを保存します

以下の例では、MicrosoftWordに依存せずにC++を使用してWordテーブルを作成できます。テーブルセル内のコンテンツをフォーマットでき、セル内に画像を追加する方法も示しました。

C++を使用してDOCXにテーブルを追加するコード

#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にテーブルを挿入する方法を学びました。

 日本語