Simple Table

Tables are used to present content in a rectangular grid. Tables, along with other content such as charts or SmartArt diagrams, are embedded in a special kind of drawing, called a graphic frame, which is contained on the slide surface.

In the GemBox.Presentation API, the table-related types are grouped in the GemBox.Presentation.Tables namespace.

The following example shows how to create and populate a simple table using the GemBox.Presentation API.

Screenshot
PowerPoint Library for C# / VB.NET Screenshot

See the full code below, use Run Example to execute.

using GemBox.Presentation;
using GemBox.Presentation.Tables;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        PresentationDocument presentation = new PresentationDocument();

        // Create new presentation slide.
        Slide slide = presentation.Slides.AddNew(SlideLayoutType.Custom);

        // Create new table.
        Table table = slide.Content.AddTable(5, 5, 20, 12, LengthUnit.Centimeter);

        // Format table with no-style grid.
        table.Format.Style = presentation.TableStyles.GetOrAdd(
            TableStyleName.NoStyleTableGrid);

        int columnCount = 4;
        int rowCount = 10;

        for (int i = 0; i < columnCount; i++)
        {
            // Create new table column.
            TableColumn column = table.Columns.AddNew(
                Length.From(5, LengthUnit.Centimeter));
        }

        for (int i = 0; i < rowCount; i++)
        {
            // Create new table row.
            TableRow row = table.Rows.AddNew(
                Length.From(1.2, LengthUnit.Centimeter));

            for (int j = 0; j < columnCount; j++)
            {
                // Create new table cell.
                TableCell cell = row.Cells.AddNew();

                // Set table cell text.
                TextRun run = cell.Text.AddParagraph().AddRun(
                    string.Format(null, "Cell {0}-{1}", i + 1, j + 1));
            }
        }

        presentation.Save("Simple Table.pptx");
    }
}
Imports GemBox.Presentation
Imports GemBox.Presentation.Tables

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim presentation As PresentationDocument = New PresentationDocument

        ' Create new presentation slide.
        Dim slide As Slide = presentation.Slides.AddNew(SlideLayoutType.Custom)

        ' Create new table.
        Dim table As Table = slide.Content.AddTable(5, 5, 20, 12, LengthUnit.Centimeter)

        ' Format table with no-style grid.
        table.Format.Style = presentation.TableStyles.GetOrAdd(
            TableStyleName.NoStyleTableGrid)

        Dim columnCount As Integer = 4
        Dim rowCount As Integer = 10

        For i As Integer = 0 To columnCount - 1
            ' Create new table column.
            Dim column As TableColumn = table.Columns.AddNew(
                Length.From(5, LengthUnit.Centimeter))
        Next

        For i As Integer = 0 To rowCount - 1
            ' Create new table row.
            Dim row As TableRow = table.Rows.AddNew(
                Length.From(1.2, LengthUnit.Centimeter))

            For j As Integer = 0 To columnCount - 1
                ' Create new table cell.
                Dim cell As TableCell = row.Cells.AddNew()

                ' Set table cell text.
                Dim run As TextRun = cell.Text.AddParagraph().AddRun(
                    String.Format(Nothing, "Cell {0}-{1}", i + 1, j + 1))
            Next
        Next

        presentation.Save("Simple Table.pptx")

    End Sub

End Module