Tables in PowerPoint are a powerful tool that allows you to present and organize data in a clear, concise, and visually appealing manner. By using tables, you can effectively communicate complex information to your audience, making it easier for them to understand and remember key points. In this article, you will learn how to insert a table into a PowerPoint Presentation in C# and VB.NET using Spire.Presentation for .NET.
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Presentation
Insert a table into a PowerPoint Presentation in C# and VB.NET
You can use the ISlide.Shapes.AppendTable(float x, float y, double[] widths, double[] heights) method to add a table to a specific slide of a PowerPoint presentation. The detailed steps are as follows:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile(string file) method.
- Get a specific slide using Presentation.Slides[int index] property.
- Define two double arrays, widths and heights, which specify the number and size of rows and columns in table.
- Add a table with the specified number and size of rows and columns to a specific position on the slide using ISlide.Shapes.AppendTable(float x, float y, double[] widths, double[] heights) method.
- Store the table data in a two-dimensional string array.
- Loop through the string array, and assign the corresponding data to each cell of the table using ITable[int columnIndex, int rowIndex].TextFrame.Text property.
- Set the alignment of the first row of the table to center.
- Apply a built-in style to the table using ITable.StylePreset property.
- Save the presentation using Presentation.SaveToFile(string file, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Presentation; namespace InsertTable { internal class Program { static void Main(string[] args) { //Initialize an instance of the Presentation class Presentation presentation = new Presentation(); //Load a PowerPoint presentation presentation.LoadFromFile(@"Input.pptx"); //Get the first slide ISlide slide = presentation.Slides[0]; //Define two double arrays, widths and heights, which specify the number and size of rows and columns in table 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 }; //Add a table with the specified number and size of rows and columns to a specific position on the slide ITable table = slide.Shapes.AppendTable(presentation.SlideSize.Size.Width / 2 - 275, 90, widths, heights); //Store table data in a two-dimensional string array string[,] data = 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", "Bogota", "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"} }; //Loop through the string array and assign data to each cell of the table for (int i = 0; i < 13; i++) for (int j = 0; j < 5; j++) { //Fill each cell of the table with data table[j, i].TextFrame.Text = data[i, j]; //Set font name and font size table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Times New Roman"); table[j, i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 16; } //Set the alignment of the first row of the table to center for (int i = 0; i < 5; i++) { table[i, 0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center; } //Apply a style to the table table.StylePreset = TableStylePreset.MediumStyle2Accent6; //Save the presentation to a file presentation.SaveToFile("InsertTable.pptx", FileFormat.Pptx2013); presentation.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.