Traverse through the cells of Table

Spire.Presentation is a powerful and easy-to-use .NET component, especially designed for developers. Using Spire.Presentation you can generate, modify, convert, render, and print documents without installing Microsoft PowerPoint on your machine. There are documents on our site introducing how to insert table and edit table in PowerPoint file. In this document, I will introduce you how to traverse through the cells of table.

It is very easy to traverse through the cells of table using Spire.Presentation. Just get the row collection using the property - TableRows of Table. Then traverse through each cell of each row. Or you can get the column collection using the property – ColumnsList of Table. Then traverse through each cell of each column.

Step 1: Create Presentation instance and load file.

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

Step 2: Get the table in PowerPoint file.

foreach (IShape shape in presentation.Slides[0].Shapes)
{
    if (shape is ITable)
    {
        table = (ITable)shape;
    }
}

Step 3: Traverse through the rows in row collection and traverse through each cell in each row.

foreach (TableRow row in table.TableRows)
{
    foreach (Cell cell in row)
    {
        //print the data in cell
        Console.Write("{0,15}", cell.TextFrame.Text);
    }
    Console.WriteLine();
}

Download and install Spire.Presentation for .NET and refer to below code to traverse through the cells in PowerPoint document.

Screenshots and Full code:

Traverse through the cells of Table

Traverse through the cells of Table

[C#]
//create Presentation instance and load file
Presentation presentation = new Presentation();
presentation.LoadFromFile("table.pptx");

ITable table = null;

//get the table
foreach (IShape shape in presentation.Slides[0].Shapes)
{
    if (shape is ITable)
    {
        table = (ITable)shape;

        //traverse through the cells of table
        foreach (TableRow row in table.TableRows)
        {
            foreach (Cell cell in row)
            {
                //print the data in cell
                Console.Write("{0,15}", cell.TextFrame.Text);
            }
            Console.WriteLine();
        }
    }
}
Console.ReadLine();

If you couldn't successfully use Spire.Presentation, please refer Spire.Presentation Quick Start which can guide you quickly use Spire.Presentation.