C#/VB.NET: Merge or Split Table Cells in Word

Merging cells means combining two or more cells into one larger cell, while splitting cells means dividing one cell into two or more smaller cells. When creating or editing tables in Microsoft Word, you may often need to merge or split table cells in order to better present your data. In this article, you will learn how to merge or split table cells in a Word document in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Merge Table Cells in Word Using C# and VB.NET

In Microsoft Word, you can merge two or more adjacent cells horizontally or vertically into a larger cell. In Spire.Doc, you can achieve the same using the Table.ApplyHorizontalMerge() or Table.ApplyVerticalMerge() method. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document by its index through Document.Sections[int] property.
  • Add a table to the section using Section.AddTable() method.
  • Specify the number of rows and columns of the table using Table.ResetCells() method.
  • Horizontally merge specific cells in the table using Table.ApplyHorizontalMerge() method.
  • Vertically merge specific cells in the table using Table.ApplyVerticalMerge() method.
  • Add some data to the table.
  • Apply a style to the table.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;

namespace MergeTableCells
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load a Word document
            document.LoadFromFile("Input.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Add a 4 x 4 table to the section
            Table table = section.AddTable();
            table.ResetCells(4, 4);

            //Horizontally merge cells 1, 2, 3, and 4 in the first row
            table.ApplyHorizontalMerge(0, 0, 3);
            //Vertically merge cells 3 and 4 in the first column
            table.ApplyVerticalMerge(0, 2, 3);

            //Add some data to the table
            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 = "Text";
                }
            }

            //Apply a style to the table
            table.ApplyStyle(DefaultTableStyle.LightGridAccent1);

            //Save the result document
            document.SaveToFile("MergeCells.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Merge or Split Table Cells in Word

Split Table Cells in Word Using C# and VB.NET

Spire.Doc for .NET offers the TableCell.SplitCell() method that enables you to split a cell in a Word table into two or more cells. The following are the detailed steps:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document by its index through Document.Sections[int] property.
  • Get a specific table in the section by its index through Section.Tables[int] property.
  • Get the table cell that you want to split through Table.Rows[int].Cells[int] property.
  • Split the cell into specific number of columns and rows using TableCell.SplitCell() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace SplitTableCells
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();
            //Load a Word Document
            document.LoadFromFile("MergeCells.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Get the first table in the section
            Table table = section.Tables[0] as Table;

            //Get the 4th cell in the 4th row
            TableCell cell1 = table.Rows[3].Cells[3];
            //Split the cell into 2 columns and 2 rows
            cell1.SplitCell(2, 2);

            //save the result document
            document.SaveToFile("SplitCells.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Merge or Split Table Cells in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.