ワープロ ドキュメントに表を追加する
このトピックでは、Open XML SDK for Office のクラスを使用して、プログラムによってワープロ ドキュメントにテーブルを追加する方法について説明します。 このタスクを示すメソッド AddTable
例が含まれています。
AddTable メソッド
AddTable
メソッドを使用して、単純なテーブルをワープロ ドキュメントに追加できます。
AddTable
メソッドは、次を示す 2 つのパラメーターを受け取ります。
変更する文書の名前 (文字列)。
文書に表として挿入される文字列の 2 次元配列。
static void AddTable(string fileName, string[,] data)
AddTable メソッドを呼び出す
AddTable
メソッドは、指定したドキュメントを変更し、指定した 2 次元配列の情報を含むテーブルを追加します。 メソッドを呼び出すには、次のコード例で示すように、両方のパラメーター値を渡します。
string fileName = args[0];
AddTable(fileName, new string[,] {
{ "Hawaii", "HI" },
{ "California", "CA" },
{ "New York", "NY" },
{ "Massachusetts", "MA" }
});
コードのしくみ
次のコードは、まず、 Open メソッドを使用してドキュメントを開き、ドキュメントを読み取り/書き込みアクセス (最終的な true
パラメーター値) で開く必要があることを示します。 次に、コードは、ワープロ ドキュメントの Document プロパティを使用して、メイン ドキュメント パーツのルート要素への参照MainDocumentPartを取得します。
using (var document = WordprocessingDocument.Open(fileName, true))
{
if (document.MainDocumentPart is null || document.MainDocumentPart.Document.Body is null)
{
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}
var doc = document.MainDocumentPart.Document;
テーブル オブジェクトを作成し、そのプロパティを設定する
ドキュメントにテーブルを挿入する前に、 Table オブジェクトを作成し、そのプロパティを設定する必要があります。 テーブルのプロパティを設定するには、 TableProperties オブジェクトの値を作成して指定します。
TableProperties
クラスは、Shading、TableBorders、TableCaption、TableCellProperties、TableJustificationなど、多くのテーブル指向プロパティを提供します。 サンプル メソッドには、以下のコードが含まれています。
Table table = new();
TableProperties props = new(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}));
table.AppendChild<TableProperties>(props);
TableProperties
クラスのコンストラクターを使用すると、必要な数の子要素 (XElement コンストラクターと同様) を指定できます。 この場合、コードによって TopBorder、 BottomBorder、 LeftBorder、 RightBorder、 InsideHorizontalBorder、 InsideVerticalBorder の子要素が作成され、それぞれがテーブルの罫線要素の 1 つを記述します。 各要素について、コードはコンストラクターの呼び出しの一環として Val
プロパティと Size
プロパティを設定します。 サイズの設定は簡単ですが、 Val
プロパティを設定するにはもう少し手間が必要です。このプロパティは、この特定のオブジェクトの境界線のスタイルを表し、列挙値に設定する必要があります。 そのためには、 EnumValue<T> ジェネリック型のインスタンスを作成し、特定の罫線型 (BorderValues) をパラメーターとしてコンストラクターに渡します。 コードが設定する必要があるすべてのテーブル罫線値を設定すると、テーブルのAppendChild メソッドが呼び出され、ジェネリック型がTablePropertiesつまり、変数 props
を値として使用して、TableProperties
クラスのインスタンスが追加されます。
テーブルにデータを入力する
表とそのプロパティを用意したら、表にデータを格納します。 サンプル プロシージャでは、最初に指定した文字列の配列内のすべてのデータ行を反復処理し、データ行ごとに新しい TableRow インスタンスを作成します。 次のコードは、行を作成してテーブルに追加する方法を示しています。 その後、列ごとに、新しい TableCell オブジェクトが作成され、データが入力され、行に追加されます。
次に、コードは以下の処理を行います。
- 文字列の配列の値を含む新しい Text オブジェクトを作成します。
- 新しいRun オブジェクトのコンストラクターにText オブジェクトを渡します。
- 新しいParagraph オブジェクトのコンストラクターにRun オブジェクトを渡します。
- Paragraph オブジェクトをセルの Append メソッドに渡します。
その後、コードは新しい TableCellProperties オブジェクトをセルに追加します。 この TableCellProperties
オブジェクトは、既に見た TableProperties
オブジェクトと同様に、コンストラクター内で指定する必要がある限り多くのオブジェクトを受け入れます。 この場合、コードは新しい TableCellWidth オブジェクトのみを渡し、その Type プロパティを TableWidthUnitValues に設定します (これにより、テーブルは各列の幅のサイズを自動的に変更します)。
for (var i = 0; i < data.GetUpperBound(0); i++)
{
var tr = new TableRow();
for (var j = 0; j < data.GetUpperBound(1); j++)
{
var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
// Assume you want columns that are automatically sized.
tc.Append(new TableCellProperties(
new TableCellWidth { Type = TableWidthUnitValues.Auto }));
tr.Append(tc);
}
table.Append(tr);
}
完了
最後の仕上げに、表を文書の本文に追加し、文書を保存します。
doc.Body.Append(table);
サンプル コード
以下に、C# と Visual Basic の完全な AddTable コード サンプルを示します。
static void AddTable(string fileName, string[,] data)
{
if (data is not null)
{
using (var document = WordprocessingDocument.Open(fileName, true))
{
if (document.MainDocumentPart is null || document.MainDocumentPart.Document.Body is null)
{
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}
var doc = document.MainDocumentPart.Document;
Table table = new();
TableProperties props = new(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}));
table.AppendChild<TableProperties>(props);
for (var i = 0; i < data.GetUpperBound(0); i++)
{
var tr = new TableRow();
for (var j = 0; j < data.GetUpperBound(1); j++)
{
var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j]))));
// Assume you want columns that are automatically sized.
tc.Append(new TableCellProperties(
new TableCellWidth { Type = TableWidthUnitValues.Auto }));
tr.Append(tc);
}
table.Append(tr);
}
doc.Body.Append(table);
}
}
}