C#: Merge or Split Tables in Word

2024-08-02 08:51:05 Written by  support iceblue
Rate this item
(0 votes)

Tables in Word documents allow users to organize data in a clear and structured manner. However, as documents grow in complexity, the need to adjust table structures often arises. Whether you need to combine multiple tables for a comprehensive view or divide a large table for better readability, mastering the art of merging and splitting tables in Word can significantly improve the presentation of your data.  In this article, you will learn how to merge or split tables in Word in C# 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 Tables in Word in C#

With Spire.Doc for .NET, you can combine two or more tables into one by copying all rows from other tables to the target table and then deleting the other tables. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get two tables in the section using section.Tables[] property.
  • Iterate through all rows in the second table and copy them using Table.Rows[].Clone() method.
  • Add the rows of the second table to the first table using Table.Rows.Add() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
using Spire.Doc;

namespace CombineTables
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("Cost.docx");

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

            //Get the first and second table in the section
            Table table1 = section.Tables[0] as Table;
            Table table2 = section.Tables[1] as Table;

            //Add the rows of table2 to table1
            for (int i = 0; i < table2.Rows.Count; i++)
            {
                table1.Rows.Add(table2.Rows[i].Clone());
            }

            //Remove the table2
            section.Tables.Remove(table2);

            //Save the result document
            doc.SaveToFile("CombineTables.docx", FileFormat.Docx);
        }
    }
}

C#: Merge or Split Tables in Word

Split Tables in Word in C#

To split a table into two or more tables, you need to create a new table, then copy the specified rows from the original table to the new table, and then delete those rows from the original table. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified table in the section using section.Tables[] property.
  • Specify the row index where the table will be split.
  • Create a new instance of the Table class.
  • Iterate through the specified rows in the original table and copy them using Table.Rows[].Clone() method.
  • Add the specified rows to the new table using Table.Rows.Add() method.
  • Iterate through the copied rows and remove each row from the original table using Table.Rows.RemoveAt() method.
  • Add the new table to the section using Section.Tables.Add() method.
  • Save the result document using Document.SaveToFile() method.
  • C#
using Spire.Doc;

namespace SplitWordTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("CombineTables.docx");

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

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

            //Specify to split the table from the fifth row
            int splitIndex = 4;

            //Create a new table
            Table newTable = new Table(section.Document);

            //Adds rows (from the 5th to the last row) to the new table
            for (int i = splitIndex; i < table.Rows.Count; i++)
            {
                newTable.Rows.Add(table.Rows[i].Clone());
            }

            //Delete rows from the original table
            for (int i = table.Rows.Count - 1; i >= splitIndex; i--)
            {
                table.Rows.RemoveAt(i);
            }

            //Add the new table to the section
            section.Tables.Add(newTable);

            //Save the result document
            doc.SaveToFile("SplitTable.docx", FileFormat.Docx);
        }
    }
}

C#: Merge or Split Tables 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.