チュートリアル

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

チュートリアル».NET»Spire.Doc for .NET»»C#/VB.NET:Word で表のセルを結合または分割する方法
2022-12-28

C#/VB.NET:Word で表のセルを結合または分割する方法

セルの結合は、2つ以上のセルを1つの大きなセルに結合することです。セルの分割は、1つのセルを2つ以上の小さなセルに分割することです。Microsoft Word で表を作成または編集する際には、データをよりよく表示するために、表のセルを結合または分割する必要があることがよくあります。この記事では、Spire.Doc for .NET を使用して、C# および VB.NET でプログラムによって Word で表のセルを結合または分割する方法を示します。

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

まず、Spire.Doc for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。

PM> Install-Package Spire.Doc

C# と VB.NET を使用して Word で表のセルを結合する

Microsoft Word では、垂直または水平に隣接する2つ以上のセルを1つの大きなセルに結合できます。Spire.Doc では、Table.ApplyHorizontalMerge()Table.ApplyVerticalMerge() メソッドを使用してセルを結合できます。詳細な手順は次のとおりです。

  • Document クラスのインスタンスを初期化します。
  • Document.LoadFromFile() メソッドを使用して Word ドキュメントをロードします。
  • Document.Sections[int] プロパティを使用して、ドキュメント内の特定のセクションをインデックスで取得します。
  • Section.AddTable() メソッドを使用してセクションに表を追加します。
  • Table.ResetCells() メソッドを使用して、表の行数と列数を指定します。
  • Table.ApplyHorizontalMerge() メソッドを使用して、表内の特定のセルを水平方向に結合します。
  • Table.ApplyVerticalMerge() メソッドを使用して、表内の特定のセルを垂直方向に結合します。
  • 表にデータを追加します。
  • 表にスタイルを適用します。
  • Document.SaveToFile() メソッドを使用して結果ドキュメントを保存します。
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace MergeTableCells
{
    class Program
    {
        static void Main(string[] args)
        {
            //Documentインスタンスを作成する
            Document document = new Document();
            //Wordドキュメントをロードする
            document.LoadFromFile("sample.docx");

            //最初のセクションを取得する
            Section section = document.Sections[0];

            //セクションに4 x 4の表を追加する
            Table table = section.AddTable();
            table.ResetCells(4, 4);

            //最初の行のセル1、2、3、4を水平に結合する
            table.ApplyHorizontalMerge(0, 0, 3);
            //最初の列のセル3と4を垂直に結合する
            table.ApplyVerticalMerge(0, 2, 3);

            //表にデータを追加する
            for (int row = 0; row < table.Rows.Count; row++)
            {
                for (int col = 0; col < table.Rows[row].Cells.Count; col++)
                {
                    TableCell cell = table[row, col];
                    cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    Paragraph paragraph = cell.AddParagraph();
                    paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center;
                    paragraph.Text = "テキスト";
                }
            }

            //表にスタイルを適用する
            table.ApplyStyle(DefaultTableStyle.LightGridAccent1);

            //結果ドキュメントを保存する
            document.SaveToFile("MergeCells.docx", FileFormat.Docx2013);
        }
    }
}
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace MergeTableCells
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Documentインスタンスを作成する
            Dim document As Document = New Document()
            'Wordドキュメントをロードする
            document.LoadFromFile("sample.docx")

            '最初のセクションを取得する
            Dim section As Section = document.Sections(0)

            'セクションに4 x 4の表を追加する
            Dim table As Table = section.AddTable()
            table.ResetCells(4, 4)

            '最初の行のセル1、2、3、4を水平に結合する
            table.ApplyHorizontalMerge(0, 0, 3)
            '最初の列のセル3と4を垂直に結合する 
            table.ApplyVerticalMerge(0, 2, 3)

            '表にデータを追加する
            For row As Integer = 0 To table.Rows.Count - 1
                For col As Integer = 0 To table.Rows(row).Cells.Count - 1
                    Dim cell As TableCell = table(row, col)
                    cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
                    Dim paragraph As Paragraph = cell.AddParagraph()
                    paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
                    paragraph.Text = "テキスト"
                Next
            Next

            '表にスタイルを適用する
            table.ApplyStyle(DefaultTableStyle.LightGridAccent1)

            '結果ドキュメントを保存する
            document.SaveToFile("MergeCells.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

C#/VB.NET:Word で表のセルを結合または分割する方法

C# と VB.NET を使用して Word で表のセルを分割する

Spire.Doc for .NET が提供する TableCell.SplitCell() メソッドは、Word で表のセルを2つ以上のセルに分割することをサポートしています。詳細な手順は次のとおりです。

  • Document クラスのインスタンスを初期化します。
  • Document.LoadFromFile() メソッドを使用して Word ドキュメントをロードします。
  • Document.Sections[int] プロパティを使用して、ドキュメント内の特定のセクションをインデックスで取得します。
  • Section.Tables[int] プロパティを使用して、セクション内の特定の表をインデックスで取得します。
  • Table.Rows[int].Cells[int] プロパティで分割する表のセルを取得します。
  • TableCell.SplitCell() メソッドを使用して、セルを特定の数の列と行に分割します。
  • Document.SaveToFile() メソッドを使用して結果ドキュメントを保存します。
  • C#
  • VB.NET
using Spire.Doc;

namespace SplitTableCells
{
    class Program
    {
        static void Main(string[] args)
        {
            //Documentインスタンスを作成する
            Document document = new Document();
            //Wordドキュメントをロードする
            document.LoadFromFile("MergeCells.docx");

            //最初のセクションを取得する
            Section section = document.Sections[0];

            //セクションの最初の表を取得する
            Table table = section.Tables[0] as Table;

            //4行の4番目のセルを取得する
            TableCell cell1 = table.Rows[3].Cells[3];
            //セルを2列2行に分割する  
            cell1.SplitCell(2, 2);

            //結果ドキュメントを保存する
            document.SaveToFile("SplitCells.docx", FileFormat.Docx2013);
        }
    }
}
Imports Spire.Doc

Namespace SplitTableCells
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Documentインスタンスを作成する
            Dim document As Document = New Document()
            'Wordドキュメントをロードする
            document.LoadFromFile("MergeCells.docx")

            '最初のセクションを取得する
            Dim section As Section = document.Sections(0)

            'セクションの最初の表を取得する
            Dim table As Table = TryCast(section.Tables(0), Table)

            '4行の4番目のセルを取得する
            Dim cell1 As TableCell = table.Rows(3).Cells(3)
            'セルを2列2行に分割する
            cell1.SplitCell(2, 2)

            '結果ドキュメントを保存する
            document.SaveToFile("SplitCells.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

C#/VB.NET:Word で表のセルを結合または分割する方法

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

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

Read 732 times