How to Insert Table to a PowerPoint Slide

A table is a great way to present data to groups, using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. As tables are widely used to display data on presentation slides, it is necessary for any PowerPoint component to own this feature. This article demonstrates how to create a table programmatically using Spire.Presentation for .NET.

Inert Table to a PPT Slide

(The table we're about to insert.)

Firstly, download Spire.Presentation and install it on system. Create a project in Visual Studio and add Spire.Presentation.dll as reference in your new project. The default folder of Spire.Presnetation.dll is "C:\Program Files\e-iceblue\Spire.Presentation\Bin". Then, let's get started with code.

Step 1: Create a new PPT document instance.

Presentation presentation = new Presentation();

Step 2: Call AppendTable() method to create a table and set the table style.

Double[] widths = new double[] { 100, 100, 150, 100, 100 };
Double[] heights = new double[] { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
ITable table = presentation.Slides[0].Shapes.AppendTable(presentation.SlideSize.Size.Width / 2 - 275, 80, widths, heights);
table.StylePreset = TableStylePreset.LightStyle1Accent2;

Step 3: Define a string array to store table data.

String[,] dataStr = new String[,]{
            {"Name",	"Capital",	"Continent",	"Area",	"Population"},
            {"Venezuela",	"Caracas",	"South America",	"912047",	"19700000"},
            {"Bolivia",	"La Paz",	"South America",	"1098575",	"7300000"},
            {"Brazil",	"Brasilia",	"South America",	"8511196",	"150400000"},
            {"Canada",	"Ottawa",	"North America",	"9976147",	"26500000"},
            {"Chile",	"Santiago",	"South America",	"756943",	"13200000"},
            {"Colombia",	"Bagota",	"South America",	"1138907",	"33000000"},
            {"Cuba",	"Havana",	"North America",	"114524",	"10600000"},
            {"Ecuador",	"Quito",	"South America",	"455502",	"10600000"},
            {"Paraguay",	"Asuncion","South America", "406576",	"4660000"},
            {"Peru",	"Lima",	"South America",	"1285215",	"21600000"},
            {"Jamaica",	"Kingston",	"North America",	"11424",	"2500000"},
            {"Mexico",	"Mexico City",	"North America",	"1967180",	"88600000"}
            };

Step 4: Fill the table with data and set the text font.

for (int i = 0; i < 13; i++)
                for (int j = 0; j < 5; j++)
                {
                    table[j, i].TextFrame.Text = dataStr[i, j];                  
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Narrow");
                }

Step 5: Set the alignment of the first row to center.

for (int i = 0; i < 5; i++)
            {
                table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
            }

Step 6: Save and preview.

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

Full C# code:

            //create PPT document
            Presentation presentation = new Presentation();
            //add new table to PPT
            
            Double[] widths = new double[] { 100, 100, 150, 100, 100 };
            Double[] heights = new double[] { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
            ITable table = presentation.Slides[0].Shapes.AppendTable(presentation.SlideSize.Size.Width / 2 - 275, 80, widths, heights);
            //set the style of table
            table.StylePreset = TableStylePreset.LightStyle1Accent2;

            String[,] dataStr = new String[,]{
            {"Name",	"Capital",	"Continent",	"Area",	"Population"},
            {"Venezuela",	"Caracas",	"South America",	"912047",	"19700000"},
            {"Bolivia",	"La Paz",	"South America",	"1098575",	"7300000"},
            {"Brazil",	"Brasilia",	"South America",	"8511196",	"150400000"},
            {"Canada",	"Ottawa",	"North America",	"9976147",	"26500000"},
            {"Chile",	"Santiago",	"South America",	"756943",	"13200000"},
            {"Colombia",	"Bagota",	"South America",	"1138907",	"33000000"},
            {"Cuba",	"Havana",	"North America",	"114524",	"10600000"},
            {"Ecuador",	"Quito",	"South America",	"455502",	"10600000"},
            {"Paraguay",	"Asuncion","South America", "406576",	"4660000"},
            {"Peru",	"Lima",	"South America",	"1285215",	"21600000"},
            {"Jamaica",	"Kingston",	"North America",	"11424",	"2500000"},
            {"Mexico",	"Mexico City",	"North America",	"1967180",	"88600000"}
            };
            for (int i = 0; i < 13; i++)
                for (int j = 0; j < 5; j++)
                {
                    //fill the table with data
                    table[j, i].TextFrame.Text = dataStr[i, j];

                    //set the Font
                    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Narrow");
                }

            //set the alignment of the first row to Center
            for (int i = 0; i < 5; i++)
            {
                table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;
            }
            //save the document
            presentation.SaveToFile("table.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("table.pptx");