Word 文書において、表はデータを整理して表示するための基本的な要素です。数値データの集計結果やスケジュール、比較表など、さまざまな場面で使用されます。文書生成をプログラムで自動化する際には、表の作成処理も併せて実装する必要が出てきます。
本記事では、Spire.Doc for C++ を使用して、C++ で Word 文書に表を作成する方法を紹介します。Spire.Doc for C++ は、Microsoft Office に依存せず Word 文書の読み込み、編集、保存を行える C++ ライブラリです。
環境構築
Spire.Doc for C++ をプロジェクトに導入する方法は主に2つあります。
-
NuGet 経由でのインストール: Visual Studio の「NuGet パッケージの管理」から
spire.doc.cppを検索してインストールします。 - 手動での追加: パッケージをダウンロードし、ヘッダーファイルとライブラリファイルをプロジェクトに手動で追加します。
導入後、以下のようにヘッダーファイルをインクルードします。
#include "Spire.Doc.o.h"
基本的な表の作成
まず、最も基本的な表の作成方法です。セクションに表を追加し、行数と列数を指定してセルにデータを入力します。
#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();
// 表を追加
intrusive_ptr<Table> table = section->AddTable(true);
// 行数と列数を指定してセルをリセット
table->ResetCells(3, 4);
// ヘッダー行
table->GetRow(0)->GetCell(0)->AddParagraph()->AppendText(L"商品名");
table->GetRow(0)->GetCell(1)->AddParagraph()->AppendText(L"数量");
table->GetRow(0)->GetCell(2)->AddParagraph()->AppendText(L"単価");
table->GetRow(0)->GetCell(3)->AddParagraph()->AppendText(L"合計");
// データ行 1
table->GetRow(1)->GetCell(0)->AddParagraph()->AppendText(L"ノート");
table->GetRow(1)->GetCell(1)->AddParagraph()->AppendText(L"10");
table->GetRow(1)->GetCell(2)->AddParagraph()->AppendText(L"150");
table->GetRow(1)->GetCell(3)->AddParagraph()->AppendText(L"1,500");
// データ行 2
table->GetRow(2)->GetCell(0)->AddParagraph()->AppendText(L"ペン");
table->GetRow(2)->GetCell(1)->AddParagraph()->AppendText(L"5");
table->GetRow(2)->GetCell(2)->AddParagraph()->AppendText(L"200");
table->GetRow(2)->GetCell(3)->AddParagraph()->AppendText(L"1,000");
// 保存
doc->SaveToFile(L"CreateTable.docx", FileFormat::Docx2013);
doc->Close();
}
このコードでは、AddTable(true) で表を追加し、ResetCells(3, 4) で3行4列の表を生成しています。各セルへのデータ入力は、GetRow(行番号)->GetCell(列番号)->AddParagraph()->AppendText() というパスで行います。行番号・列番号は0から始まるインデックスです。
表のスタイルを設定する
表全体にスタイルを適用することで、見た目を一括で整えることができます。Spire.Doc for C++ では、定義済みの表スタイルを ApplyStyle で適用できます。
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
intrusive_ptr<Document> doc = new Document();
intrusive_ptr<Section> section = doc->AddSection();
intrusive_ptr<Table> table = section->AddTable(true);
table->ResetCells(4, 3);
// 表にデータを入力
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
table->GetRow(i)->GetCell(j)->AddParagraph()->AppendText(
(wstring(L"項目") + to_wstring(i + 1) + L"-" + to_wstring(j + 1)).c_str()
);
}
}
// 定義済みの表スタイルを適用
table->ApplyStyle(DefaultTableStyle::LightGridAccent1);
doc->SaveToFile(L"TableWithStyle.docx", FileFormat::Docx2013);
doc->Close();
}
DefaultTableStyle 列挙体には LightGridAccent1 のほか、MediumShading1Accent1、DarkListAccent1 など、多数の定義済みスタイルが用意されています。文書の目的に合わせて選択できます。
セルの結合
表の一部のセルを結合して、ヘッダー領域を広げたり、項目をグループ化したりすることも可能です。ApplyHorizontalMerge および ApplyVerticalMerge メソッドを使用します。
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
intrusive_ptr<Document> doc = new Document();
intrusive_ptr<Section> section = doc->AddSection();
intrusive_ptr<Table> table = section->AddTable(true);
table->ResetCells(3, 4);
// 1行目の先頭セルに「商品一覧」と入力
table->GetRow(0)->GetCell(0)->AddParagraph()->AppendText(L"商品一覧");
// 行0の列0から列3までを水平方向に結合
table->ApplyHorizontalMerge(0, 0, 3);
// データ行
table->GetRow(1)->GetCell(0)->AddParagraph()->AppendText(L"ノート");
table->GetRow(1)->GetCell(1)->AddParagraph()->AppendText(L"A4");
table->GetRow(1)->GetCell(2)->AddParagraph()->AppendText(L"150円");
table->GetRow(1)->GetCell(3)->AddParagraph()->AppendText(L"在庫あり");
table->GetRow(2)->GetCell(0)->AddParagraph()->AppendText(L"ペン");
table->GetRow(2)->GetCell(1)->AddParagraph()->AppendText(L"黒");
table->GetRow(2)->GetCell(2)->AddParagraph()->AppendText(L"200円");
table->GetRow(2)->GetCell(3)->AddParagraph()->AppendText(L"在庫あり");
doc->SaveToFile(L"MergeCells.docx", FileFormat::Docx2013);
doc->Close();
}
ApplyHorizontalMerge(行番号, 開始列, 終了列) で水平方向のセル結合を行います。同様に、ApplyVerticalMerge(列番号, 開始行, 終了行) で垂直方向のセル結合が可能です。
表の罫線を設定する
表の罫線をカスタマイズするには、TableFormat クラスのプロパティを使用します。罫線のスタイル、色、太さを設定できます。
#include "Spire.Doc.o.h"
using namespace Spire::Doc;
using namespace std;
int main()
{
intrusive_ptr<Document> doc = new Document();
intrusive_ptr<Section> section = doc->AddSection();
intrusive_ptr<Table> table = section->AddTable(true);
table->ResetCells(3, 3);
// セルにデータを入力
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
table->GetRow(i)->GetCell(j)->AddParagraph()->AppendText(
(wstring(L"セル") + to_wstring(i + 1) + L"-" + to_wstring(j + 1)).c_str()
);
}
}
// 罫線の設定
table->GetTableFormat()->SetBorders(BorderStyle::Single);
table->GetTableFormat()->GetBorders()->SetColor(Color::GetDarkBlue());
table->GetTableFormat()->GetBorders()->SetLineWidth(1.5);
doc->SaveToFile(L"TableBorders.docx", FileFormat::Docx2013);
doc->Close();
}
SetBorders で罫線のスタイル(Single、Double、Dashed など)を指定し、SetColor で色、SetLineWidth で線の太さをポイント単位で設定します。
注意点
- 表の操作では、行や列のインデックスは0から始まります。
ResetCells(3, 4)と指定した場合、行は0~2、列は0~3のインデックスでアクセスします。 - 本ライブラリは Microsoft Office のインストールを必要としませんが、商用利用の場合はライセンスが必要です。利用前にライセンス条項を確認してください。
- 大量のデータを含む表を生成する場合、ループ処理によるセルへのアクセスが繰り返されるため、処理時間とメモリ使用量に注意してください。
おわりに
本記事では、Spire.Doc for C++ を用いて Word 文書に表を作成する方法として、基本的な表の作成、スタイルの適用、セルの結合、罫線の設定を紹介しました。いずれも Section->AddTable() で表を追加した後、ResetCells でサイズを指定し、各セルにテキストを追加する流れで実装できます。表の操作が必要な文書処理の場面で、一つの手段として参考にしてください。