MS Word では、表によってデータを行と列に整理して表示することができるので、情報を理解しやすく、分析しやすくすることができます。この記事では、Spire.Doc for .NET を使って、Word ドキュメント内のデータで表を作成する方法を説明します。 Spire.Doc for .NET をインストールする まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。 PM> Install-Package Spire.Doc Word ドキュメントで表を作成する 以下は、Word ドキュメントで表を作成し、書式設定するために Spire.Doc for .NET が提供する主要なクラスとメソッドの一部です。 名前 説明 Table クラス Word ドキュメント内の表を表します。 TableRow クラス 表中の行を表します。 TableCell クラス 表中の特定のセルを表します。 Section.AddTbale() メソッド 指定されたセクションに新しい表を追加するために使用します。 Table.ResetCells() メソッド 行番号、列番号のリセットに使用します。 Table.Rows プロパティ 表の行を取得するために使用します。 TableRow.Height プロパティ 指定された行の高さを設定するために使用します。 TableRow.Cells プロパティ セル集を返します。 TableRow.RowFormat プロパティ 指定された行の書式を取得するために使用します。 詳細な手順は以下の通りです。 Document クラスのオブジェクトを作成し、それにセクションを追加します。 ヘッダー行とその他の行のデータを用意し、それぞれ一次元の文字列配列と二次元の文字列配列に格納します。 Section.AddTable() メソッドを使用して、セクションに表を追加します。 ヘッダー行にデータを挿入し、行の高さ、背景色、テキスト配置などの行の書式を設定します。 その他の行にデータを挿入し、書式を設定します。 Document.SaveToFile() メソッドを使用して、ドキュメントを保存します。 C# VB.NET using System; using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace WordTable { class Program { static void Main(string[] args) { //Documentクラスのオブジェクトを作成する Document doc = new Document(); //セクションを追加する Section s = doc.AddSection(); //表のデータを定義する String[] Header = { "日付", "商品名", "販売地域", "在庫量", "注文量" }; String[][] data = { new String[]{ "08/07/2021", "カヤック", "米国", "24","16"}, new String[]{ "08/07/2021", "潜水機", "米国", "5","3"}, new String[]{ "08/07/2021", "制御システム", "チェコ共和国", "165","216"}, new String[]{ "08/08/2021", "二段レギュレータ", "米国", "98","88"}, new String[]{ "08/08/2021", "個人用潜水ソナー", "米国", "46","45"}, new String[]{ "08/09/2021", "コンパスコンソール", "米国", "211","300"}, new String[]{ "08/09/2021", "制御システム", "イギリス", "166","100"}, new String[]{ "08/10/2021", "予備のエアダンサ", "イギリス", "47","43"}, }; //表を追加する Table table = s.AddTable(true); table.ResetCells(data.Length + 1, Header.Length); //最初の行を表のヘッダーとして設定する TableRow FRow = table.Rows[0]; FRow.IsHeader = true; //1列目の高さと色を設定する FRow.Height = 23; FRow.RowFormat.BackColor = Color.LightSeaGreen; for (int i = 0; i < Header.Length; i++) { //セルの配置を設定する Paragraph p = FRow.Cells[i].AddParagraph(); FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; p.Format.HorizontalAlignment = HorizontalAlignment.Center; //データのフォーマットを設定する TextRange TR = p.AppendText(Header[i]); TR.CharacterFormat.FontName = "Calibri"; TR.CharacterFormat.FontSize = 12; TR.CharacterFormat.Bold = true; } //他の行にデータを追加し、セルの書式を設定する for (int r = 0; r < data.Length; r++) { TableRow DataRow = table.Rows[r + 1]; DataRow.Height = 20; for (int c = 0; c < data[r].Length; c++) { DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; Paragraph p2 = DataRow.Cells[c].AddParagraph(); TextRange TR2 = p2.AppendText(data[r][c]); p2.Format.HorizontalAlignment = HorizontalAlignment.Center; //データの書式を設定する TR2.CharacterFormat.FontName = "Calibri"; TR2.CharacterFormat.FontSize = 11; } } //ドキュメントを保存する doc.SaveToFile("表の追加.docx", FileFormat.Docx2013); } } } Imports System.Drawing Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace WordTable Class Program Shared Sub Main(ByVal args() As String) 'Documentクラスのオブジェクトを作成する Dim doc As Document = New Document() 'セクションを追加する Dim s As Section = doc.AddSection() '表のデータを定義する Dim Header() As String = {"日付", "商品名", "販売地域", "在庫量", "注文量"} Dim data()() As String = {New String() {"08/07/2021", "カヤック", "米国", "24", "16"} , New String() { "08/07/2021", "潜水機", "米国", "5","3" } , New String() { "08/07/2021", "制御システム", "チェコ共和国", "165","216" } , New String() { "08/08/2021", "二段レギュレータ", "米国", "98","88" } , New String() { "08/08/2021", "個人用潜水ソナー", "米国", "46","45" } , New String() { "08/09/2021", "コンパスコンソール", "米国", "211","300" } , New String() { "08/09/2021", "制御システム", "イギリス", "166","100" } , New String() { "08/10/2021", "予備のエアダンサ", "イギリス", "47","43" } , } '表を追加する Dim table As Table = s.AddTable(True) table.ResetCells(data.Length + 1, Header.Length) '最初の行を表のヘッダーとして設定する Dim FRow As TableRow = table.Rows(0) FRow.IsHeader = True '1列目の高さと色を設定する FRow.Height = 23 FRow.RowFormat.BackColor = Color.LightSeaGreen Dim i As Integer For i = 0 To Header.Length - 1 Step i + 1 'セルの配置を設定する Dim p As Paragraph = FRow.Cells(i).AddParagraph() FRow.Cells(i).CellFormat.VerticalAlignment = VerticalAlignment.Middle p.Format.HorizontalAlignment = HorizontalAlignment.Center 'データのフォーマットを設定する Dim TR As TextRange = p.AppendText(Header(i)) TR.CharacterFormat.FontName = "Yu Gothic UI" TR.CharacterFormat.FontSize = 12 TR.CharacterFormat.Bold = True Next '他の行にデータを追加し、セルの書式を設定する Dim r As Integer For r = 0 To data.Length - 1 Step r + 1 Dim DataRow As TableRow = table.Rows(r + 1) DataRow.Height = 20 Dim c As Integer For c = 0 To data(r).Length - 1 Step c + 1 DataRow.Cells(c).CellFormat.VerticalAlignment = VerticalAlignment.Middle Dim p2 As Paragraph = DataRow.Cells(c).AddParagraph() Dim TR2 As TextRange = p2.AppendText(data(r)(c)) p2.Format.HorizontalAlignment = HorizontalAlignment.Center 'データの書式を設定する TR2.CharacterFormat.FontName = "Yu Mincho" TR2.CharacterFormat.FontSize = 11 Next Next 'ドキュメントを保存する doc.SaveToFile("表の追加.docx", FileFormat.Docx2013) End Sub End Class End Namespace 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。