チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»C++»Spire.Doc for C++»»C++:Word ドキュメントに表を作成する方法
2023-04-23

C++:Word ドキュメントに表を作成する方法

表は、データを整理して表示するための強力なツールです。データを行と列に並べることで、著者は異なるデータ区分間の関係を説明し、読者は複雑なデータを理解し分析することが容易になります。この記事では、Spire.Doc for C++ を使用して、プログラムで Word ドキュメントに表を作成する方法について説明します。

Spire.Doc for C++ をインストールします

Spire.Doc for C++ をアプリケーションに組み込むには、2つの方法があります。一つは NuGet 経由でインストールする方法、もう一つは当社のウェブサイトからパッケージをダウンロードし、ライブラリをプログラムにコピーする方法です。NuGet 経由のインストールの方が便利で、より推奨されます。詳しくは、以下のリンクからご覧いただけます。

Spire.Doc for C++ を C++ アプリケーションに統合する方法

Word ドキュメントに表を作成する

Spire.Doc for C++ では、Word ドキュメントのセクションに表を追加するための Section->AddTable() メソッドを用意しています。詳しい手順は以下の通りです。

  • Document のインスタンスを作成します。
  • Document->AddSection() メソッドを使用して、ドキュメントにセクションを追加します。
  • ヘッダー行と残りの行のデータを定義し、それぞれ1次元ベクトルと2次元ベクトルに格納します。
  • Section->AddTable() メソッドを使用して、セクションに表を追加します。
  • Table->ResetCells(int, int) メソッドを使用して、表の行数、列数を指定します。
  • ヘッダー行に1次元ベクトルのデータを追加し、書式を設定します。
  • 残りの行に2次元ベクトルのデータを追加し、書式を設定します。
  • Document->SaveToFile() メソッドを使用して、ドキュメントを保存します。
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace std;

int main()
{
	//Documentのインスタンスを作成する
	intrusive_ptr<Document> doc = new Document();

	//ドキュメントにセクションを追加する
	intrusive_ptr<Section> section = doc->AddSection();

	//セクションのページ余白を設定する
	section->GetPageSetup()->GetMargins()->SetAll(72);

	//ヘッダー行のデータを定義する
	vector<wstring> header = { L"国名", L"首都", L"大陸", L"面積", L"人口" };

	//残りの行のデータを定義する
	vector<vector<wstring>> data =
	{
		{L"Argentina", L"Buenos Aires", L"南アメリカ", L"2777815", L"32300003"},
		{L"Bolivia", L"La Paz", L"南アメリカ", L"1098575", L"7300000"},
		{L"Brazil", L"Brasilia", L"南アメリカ", L"8511196", L"150400000"},
		{L"Canada", L"Ottawa", L"北アメリカ", L"9976147", L"26500000"},
		{L"Chile", L"Santiago", L"南アメリカ", L"756943", L"13200000"},
		{L"Colombia", L"Bogota", L"南アメリカ", L"1138907", L"33000000"},
		{L"Cuba", L"Havana", L"北アメリカ", L"114524", L"10600000"},
		{L"Ecuador", L"Quito", L"南アメリカ", L"455502", L"10600000"},
		{L"El Salvador", L"San Salvador", L"北アメリカ", L"20865", L"5300000"},
		{L"Guyana", L"Georgetown", L"南アメリカ", L"214969", L"800000"},
		{L"Jamaica", L"Kingston", L"北アメリカ", L"11424", L"2500000"},
		{L"Mexico", L"Mexico City", L"北アメリカ", L"1967180", L"88600000"},
		{L"Nicaragua", L"Managua", L"北アメリカ", L"139000", L"3900000"},
		{L"Paraguay", L"Asuncion", L"南アメリカ", L"406576", L"4660000"},
		{L"Peru", L"Lima", L"南アメリカ", L"1285215", L"21600000"},
		{L"United States", L"Washington", L"北アメリカ", L"9363130", L"249200000"},
		{L"Uruguay", L"Montevideo", L"南アメリカ", L"176140", L"3002000"},
		{L"Venezuela", L"Caracas", L"南アメリカ", L"912047", L"19700000"}
	};

	//セクションに表を追加する
	intrusive_ptr<Table> table = section->AddTable(true);

	//表の行数、列数を指定する
	table->ResetCells(data.size() + 1, header.size());

	//最初の行をヘッダー行として設定する
	intrusive_ptr<TableRow> row = table->GetRows()->GetItemInRowCollection(0);
	row->SetIsHeader(true);

	//ヘッダー列の高さと背景色を設定する
	row->SetHeight(20);
	row->SetHeightType(TableRowHeightType::Exactly);
	row->GetRowFormat()->SetBackColor(Color::FromArgb(142, 170, 219));

	//ヘッダー行にデータを追加し、書式を設定する
	for (int i = 0; i < header.size(); i++)
	{
		//段落を追加する
		intrusive_ptr<Paragraph> p1 = row->GetCells()->GetItemInCellCollection(i)->AddParagraph();
		//配置を設定する
		p1->GetFormat()->SetHorizontalAlignment(HorizontalAlignment::Center);
		row->GetCells()->GetItemInCellCollection(i)->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
		//データを追加する
		intrusive_ptr<TextRange> tR1 = p1->AppendText(header[i].c_str());
		//データの書式を設定する
		tR1->GetCharacterFormat()->SetFontName(L"Yu Gothic UI");
		tR1->GetCharacterFormat()->SetFontSize(12);
		tR1->GetCharacterFormat()->SetBold(true);
	}

	//残りの行にデータを追加し、書式を設定する
	for (int r = 0; r < data.size(); r++)
	{
		//残りの行の高さを設定する
		intrusive_ptr<TableRow> dataRow = table->GetRows()->GetItemInRowCollection(r + 1);
		dataRow->SetHeight(20);
		dataRow->SetHeightType(TableRowHeightType::Exactly);

		for (int c = 0; c < data[r].size(); c++)
		{
			//段落を追加する
			intrusive_ptr<Paragraph> p2 = dataRow->GetCells()->GetItemInCellCollection(c)->AddParagraph();
			//配置を設定する
			dataRow->GetCells()->GetItemInCellCollection(c)->GetCellFormat()->SetVerticalAlignment(VerticalAlignment::Middle);
			//データを追加する
			intrusive_ptr<TextRange> tR2 = p2->AppendText(data[r][c].c_str());
			//データの書式を設定する
			tR2->GetCharacterFormat()->SetFontName(L"Yu Gothic UI");
			tR2->GetCharacterFormat()->SetFontSize(11);
		}
	}

	//ドキュメントを保存する
	doc->SaveToFile(L"表の作成.docx", FileFormat::Docx2013);
	doc->Close();
}

C++:Word ドキュメントに表を作成する方法

Word ドキュメントにネストされた表を作成する

Spire.Doc for C++ では、特定の表セルにネストした表を追加する TableCell->AddTable() メソッドを用意しています。詳しい手順は以下の通りです。

  • Document のインスタンスを作成します。
  • Document->AddSection() メソッドを使用して、ドキュメントにセクションを追加します。
  • Section.AddTable() メソッドを使用して、セクションに表を追加します。
  • Table->ResetCells(int, int) メソッドを使用して、表の行と列の数を指定します。
  • 表の行を取得し、各行のセルにデータを追加します。
  • TableCell->AddTable() メソッドを使用して、特定の表セルにネストした表を追加します。
  • ネストした表の行数、列数を指定します。
  • ネストされた表の行を取得し、各行のセルにデータを追加します。
  • Document->SaveToFile() メソッドを使用して、ドキュメントを保存します。
  • C++
#include "Spire.Doc.o.h"

using namespace Spire::Doc;
using namespace std;

int main()
{
	//Documentのインスタンスを作成する
	intrusive_ptr<Document> doc = new Document();

	//ドキュメントにセクションを追加する
	intrusive_ptr<Section> section = doc->AddSection();

	//セクションのページ余白を設定する
	section->GetPageSetup()->GetMargins()->SetAll(72);

	//セクションに表を追加する
	intrusive_ptr<Table> table = section->AddTable(true);

	//表の行数、列数を設定する
	table->ResetCells(2, 2);

	//表の幅をウィンドウに自動調整する
	table->AutoFit(AutoFitBehaviorType::AutoFitToWindow);

	//表の行を取得する
	intrusive_ptr<TableRow> row1 = table->GetRows()->GetItemInRowCollection(0);
	intrusive_ptr<TableRow> row2 = table->GetRows()->GetItemInRowCollection(1);

	//表のセルにデータを追加する
	intrusive_ptr<TableCell> cell1 = row1->GetCells()->GetItemInCellCollection(0);
	intrusive_ptr<TextRange> tR = cell1->AddParagraph()->AppendText(L"製品");
	tR->GetCharacterFormat()->SetFontSize(13);
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> cell2 = row1->GetCells()->GetItemInCellCollection(1);
	tR = cell2->AddParagraph()->AppendText(L"説明");
	tR->GetCharacterFormat()->SetFontSize(13);
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> cell3 = row2->GetCells()->GetItemInCellCollection(0);
	cell3->AddParagraph()->AppendText(L"Spire.Doc for C++");
	intrusive_ptr<TableCell> cell4 = row2->GetCells()->GetItemInCellCollection(1);
	cell4->AddParagraph()->AppendText(L"Spire.Doc for C++は、C++アプリケーションで"
		L"Word文書を高速かつ高品質に作成、読み取り、書き込み、変換するために"
		L"開発者向けに特別に設計されたプロフェッショナルなWordライブラリです。");

	//表の文字のフォントを設定する
	for (int m = 0; m < table->GetRows()->GetCount(); m++)
	{
		intrusive_ptr<TableRow> tableRow = table->GetRows()->GetItemInRowCollection(m);
		for (int n = 0; n < tableRow->GetCells()->GetCount(); n++)
		{
			intrusive_ptr<TableCell> nestedTableCell = tableRow->GetCells()->GetItemInCellCollection(n);
			nestedTableCell->GetFirstParagraph()->GetStyle()->GetCharacterFormat()->SetFontName(L"Yu Gothic UI");
		}
	}

	//4つ目のセルにネストした表を追加する
	intrusive_ptr<Table> nestedTable = cell4->AddTable(true);

	//ネストした表の行数、列数を設定する
	nestedTable->ResetCells(3, 2);

	//表の幅をコンテンツに自動調整する
	nestedTable->AutoFit(AutoFitBehaviorType::AutoFitToContents);

	//表の行を取得する
	intrusive_ptr<TableRow> nestedRow1 = nestedTable->GetRows()->GetItemInRowCollection(0);
	intrusive_ptr<TableRow> nestedRow2 = nestedTable->GetRows()->GetItemInRowCollection(1);
	intrusive_ptr<TableRow> nestedRow3 = nestedTable->GetRows()->GetItemInRowCollection(2);

	//ネストした表のセルにデータを追加する
	intrusive_ptr<TableCell> nestedCell1 = nestedRow1->GetCells()->GetItemInCellCollection(0);
	tR = nestedCell1->AddParagraph()->AppendText(L"項目");
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> nestedCell2 = nestedRow1->GetCells()->GetItemInCellCollection(1);
	tR = nestedCell2->AddParagraph()->AppendText(L"価格");
	tR->GetCharacterFormat()->SetBold(true);
	intrusive_ptr<TableCell> nestedCell3 = nestedRow2->GetCells()->GetItemInCellCollection(0);
	nestedCell3->AddParagraph()->AppendText(L"Developer Subscription");
	intrusive_ptr<TableCell> nestedCell4 = nestedRow2->GetCells()->GetItemInCellCollection(1);
	nestedCell4->AddParagraph()->AppendText(L"$999");
	intrusive_ptr<TableCell> nestedCell5 = nestedRow3->GetCells()->GetItemInCellCollection(0);
	nestedCell5->AddParagraph()->AppendText(L"Developer OEM Subscription");
	intrusive_ptr<TableCell> nestedCell6 = nestedRow3->GetCells()->GetItemInCellCollection(1);
	nestedCell6->AddParagraph()->AppendText(L"$2999");

	//ドキュメントを保存する
	doc->SaveToFile(L"ネストされた表の作成.docx", FileFormat::Docx2013);
	doc->Close();
}

C++:Word ドキュメントに表を作成する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 473 times