Create and Manipulate Tables in PowerPoint C#

Tables are used to arrange the data in the form of rows and columns. Moreover, they organize and summarize the data so that it can be viewed and analyzed easily. MS PowerPoint also allows you to insert tables in the presentations. Accordingly, this article covers how to create and manipulate tables in PowerPoint presentations using C#.

C# API to Create and Manipulate Tables in PowerPoint

To create and manipulate tables in PowerPoint presentations, we will use Aspose.Slides for .NET. The API lets you create, manipulate and convert PowerPoint and OpenOffice documents. You can download the API’s DLL and add a reference to it in your project. Also, you can install it using NuGet.

PM> Install-Package Aspose.Slides.NET

Create a Table in PowerPoint Presentations using C#

Creating a table using Aspose.Slides for .NET is a piece of cake. The following steps show how to create a table in a PowerPoint presentation using C#.

  • First, create a new presentation or load an existing one using Presentation class.
  • Then, get reference of the desired slide into an ISlide object.
  • Define the width and height of columns and rows respectively in double[] arrays.
  • Insert a new table in presentation using ISlide.Shapes.AddTable() method.
  • Get reference of the newly created table in an ITable object.
  • Create a loop to iterate through the rows of the table.
  • Create a nested loop to iterate through the cells of the table and in each iteration, perform the following operations.
  • Finally, save the presentation using Presentation.Save(String, SaveFormat) method.

The following code sample shows how to create a table in a PowerPoint presentation.

// Create or load presentation
Presentation pres = new Presentation();
// Access first slide
ISlide sld = pres.Slides[0];
// Define columns with widths and rows with heights
double[] dblCols = { 50, 50, 50 };
double[] dblRows = { 50, 30, 30, 30, 30 };
// Add table shape to slide
ITable tbl = sld.Shapes.AddTable(100, 50, dblCols, dblRows);
// Set border format and text for each cell
for (int row = 0; row < tbl.Rows.Count; row++)
{
for (int cell = 0; cell < tbl.Rows[row].Count; cell++)
{
// Add text to the cell
tbl.Rows[row][cell].TextFrame.Text = "Cells_" + cell;
tbl.Rows[row][cell].CellFormat.BorderTop.FillFormat.FillType = FillType.Solid;
tbl.Rows[row][cell].CellFormat.BorderTop.FillFormat.SolidFillColor.Color = Color.Red;
tbl.Rows[row][cell].CellFormat.BorderTop.Width = 5;
tbl.Rows[row][cell].CellFormat.BorderBottom.FillFormat.FillType = (FillType.Solid);
tbl.Rows[row][cell].CellFormat.BorderBottom.FillFormat.SolidFillColor.Color= Color.Red;
tbl.Rows[row][cell].CellFormat.BorderBottom.Width =5;
tbl.Rows[row][cell].CellFormat.BorderLeft.FillFormat.FillType = FillType.Solid;
tbl.Rows[row][cell].CellFormat.BorderLeft.FillFormat.SolidFillColor.Color =Color.Red;
tbl.Rows[row][cell].CellFormat.BorderLeft.Width = 5;
tbl.Rows[row][cell].CellFormat.BorderRight.FillFormat.FillType = FillType.Solid;
tbl.Rows[row][cell].CellFormat.BorderRight.FillFormat.SolidFillColor.Color = Color.Red;
tbl.Rows[row][cell].CellFormat.BorderRight.Width = 5;
}
}
// Save PPTX to disk
pres.Save("table.pptx", SaveFormat.Pptx);
view raw create-table.cs hosted with ❤ by GitHub

The following screenshot shows the table that we have created using the above code.

Create Table in PowerPoint C#

Access a Table in a Presentation using C#

You can also access the tables in the existing PowerPoint presentations and manipulate them as required. The following are the steps to access the tables in a presentation.

The following code sample shows how to access tables in a PowerPoint presentation using C#.

// Load presentation
using (Presentation pres = new Presentation("UpdateExistingTable.pptx"))
{
// Access the first slide
ISlide sld = pres.Slides[0];
// Initialize null TableEx
ITable tbl = null;
// Iterate through the shapes and set a reference to the table found
foreach (IShape shp in sld.Shapes)
if (shp is ITable)
tbl = (ITable)shp;
// Set the text of the first column of second row
tbl[0, 1].TextFrame.Text = "New";
//Write the PPTX to Disk
pres.Save("table1_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}
view raw access-table.cs hosted with ❤ by GitHub

Format Text in PowerPoint Tables using C#

Aspose.Slides for .NET also allows you to set formatting of the tables quite easily, as demonstrated in the steps below.

The following code sample shows how to set the formatting of the table in PowerPoint using C#.

// Create or load presentation
Presentation presentation = new Presentation();
// Get reference of the slide
ISlide slide = presentation.Slides[0];
// Get reference of the table
ITable someTable = presentation.Slides[0].Shapes[0] as ITable; // let's say that the first shape on the first slide is a table
// Set table cells' font height
PortionFormat portionFormat = new PortionFormat();
portionFormat.FontHeight = 25;
someTable.SetTextFormat(portionFormat);
// Set table cells' text alignment and right margin in one call
ParagraphFormat paragraphFormat = new ParagraphFormat();
paragraphFormat.Alignment = TextAlignment.Right;
paragraphFormat.MarginRight = 20;
someTable.SetTextFormat(paragraphFormat);
// Set table cells' text vertical type
TextFrameFormat textFrameFormat = new TextFrameFormat();
textFrameFormat.TextVerticalType = TextVerticalType.Vertical;
someTable.SetTextFormat(textFrameFormat);
// Save presentation
presentation.Save("result.pptx", SaveFormat.Pptx);
view raw format-table.cs hosted with ❤ by GitHub

Lock Aspect Ratio of Tables in PowerPoint using C#

You can also lock the aspect ratio of the tables in PowerPoint presentations using C#. The following are the steps to achieve this.

The following code sample shows how to lock the aspect ratio of the table in PowerPoint presentations.

// Load presentation
using (Presentation pres = new Presentation("presentation.pptx"))
{
// Get reference of the table
ITable table = (ITable)pres.Slides[0].Shapes[0];
Console.WriteLine($"Lock aspect ratio set: {table.ShapeLock.AspectRatioLocked}");
// Lock aspect ratio
table.ShapeLock.AspectRatioLocked = !table.ShapeLock.AspectRatioLocked; // invert
Console.WriteLine($"Lock aspect ratio set: {table.ShapeLock.AspectRatioLocked}");
// Save presentation
pres.Save("pres-out.pptx", SaveFormat.Pptx);
}

Get a Free API License

You can use Aspose.Slides for .NET without evaluation limitations by getting a free temporary license.

Conclusion

In this article, you have learned how to create tables in PowerPoint presentations using C#. Moreover, you have seen how to access and manipulate existing tables in the PowerPoint presentations programmatically. In addition, you can visit the documentation to explore more about Aspose.Slides for .NET. Also, you can ask your questions via our forum.

See Also