News Category

Merge table cells on PowerPoint Slide

2014-07-25 07:07:42 Written by  support iceblue
Rate this item
(0 votes)

A table, as a great way to present data into groups, always plays an important role in any type of electronic documents. A well formatted table must contains larger or smaller cells without influencing the entire row or colum - and that's something that can be easily achieved by merging or splitting cells in your existing table.

In the next section, we will introduce you how to merge cells on en existing table which is embedded on a PowerPoint slide using Spire.Presentation for .NET. By convention, we need to download and install Spire.Presentation first, add its dll as a reference in you Visual C# or VB.NET project.

Assume you got a sample PPT file which contains a table like this, obviously you can merge cell 2 and cell 3 in the first column to display the account information more clearly.

Merge table cells on PowerPoint Slide

How to achieve this programmatically using Spire.Presentation?

Step 1: Create a PPT document and load the sample file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("table.pptx");

Step 2: Get the table and merge the second row and third row of the first column.

ITable table = null;

foreach (IShape shape in presentation.Slides[0].Shapes)
{
  if (shape is ITable)
    {
      table = (ITable)shape;
      table.MergeCells(table[0, 1], table[0, 2], false);

    }
}

Step 3: Save and launch the file.

presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("result.pptx");

Result:

Merge table cells on PowerPoint Slide

Full code:

[C#]
using Spire.Presentation;

namespace MergeCells
{

    class Program
    {

        static void Main(string[] args)
        {
            // create a PPT document and load file
            Presentation presentation = new Presentation();
            presentation.LoadFromFile("table.pptx");
            // get the table in PPT document
            ITable table = null;
            foreach (IShape shape in presentation.Slides[0].Shapes)
            {
                if (shape is ITable)
                {
                    table = (ITable)shape;
                    //merge the second row and third row of the first column
                    table.MergeCells(table[0, 1], table[0, 2], false);

                }
            }

            // save the document
            presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("result.pptx");

        }

    }
}
[VB.NET]
Imports Spire.Presentation

Namespace MergeCells

	Class Program

		Private Shared Sub Main(args As String())
			' create a PPT document and load file
			Dim presentation As New Presentation()
			presentation.LoadFromFile("table.pptx")
			' get the table in PPT document
			Dim table As ITable = Nothing
			For Each shape As IShape In presentation.Slides(0).Shapes
				If TypeOf shape Is ITable Then
					table = DirectCast(shape, ITable)
					'merge the second row and third row of the first column

					table.MergeCells(table(0, 1), table(0, 2), False)
				End If
			Next

			' save the document
			presentation.SaveToFile("result.pptx", FileFormat.Pptx2010)
			System.Diagnostics.Process.Start("result.pptx")

		End Sub

	End Class
End Namespace

Additional Info

  • tutorial_title:
Last modified on Friday, 24 September 2021 09:21