How to add a row to an existing table in PowerPoint documents
Spire.Presentation is a powerful and standalone .NET component which designed for developers to operate the PowerPoint documents in C# and VB.NET. Spire.Presentation enable developers to insert a new table, remove rows or columns in an existing table, and remove the whole table from the presentation slides. This article we will show you how to add a row to an existing table in presentation slide by using C# code.
Step 1: Create Presentation instance and load file.
Presentation ppt = new Presentation(); ppt.LoadFromFile("table.pptx");
Step 2: Get the table within the PowerPoint document.
ITable table = ppt.Slides[0].Shapes[4] as ITable;
Step 3: Add a new row into the PowerPoint table and set the data for the cells in the new row.
//Get the first row TableRow row = table.TableRows[0]; //Clone the row and add it to the end of table table.TableRows.Append(row); int rowCount = table.TableRows.Count; //Get the last row TableRow lastRow = table.TableRows[rowCount - 1]; //Set new data of the first cell of last row lastRow[0].TextFrame.Text = " The first cell"; //Set new data of the second cell of last row lastRow[1].TextFrame.Text = " The second cell";
Step 4: Save the document and preview it.
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx");
Effective screenshot:
Full codes:
using Spire.Presentation; namespace AddRow { class Program { static void Main(string[] args) { Presentation ppt = new Presentation(); ppt.LoadFromFile("table.pptx"); ITable table = ppt.Slides[0].Shapes[4] as ITable; TableRow row = table.TableRows[0]; table.TableRows.Append(row); int rowCount = table.TableRows.Count; TableRow lastRow = table.TableRows[rowCount - 1]; lastRow[0].TextFrame.Text = "The first cell"; lastRow[1].TextFrame.Text = "The second cell"; ppt.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } } }
How to Apply Built-in Style to PowerPoint Table in C#, VB.NET
Built-in table styles are predefined formatting options that can be quickly applied to any table, greatly enhancing its appearance and readability. As same as MS PowerPoint, Spire.Presentation provides such a feature to give your table a professional look by applying a certain built-in table style. This article presents how this purpose can be achieved using Spire.Presentation with C#, VB.NET.
As we can see from below picture, plain tale is not that attractive before applying any style.
Code Snippet for Applying Table Style:
Step 1: Create an instance of presentation and load the test file.
Presentation ppt = new Presentation("test.pptx", FileFormat.Pptx2010);
Step 2: Get the table from presentation slide.
ITable table = ppt.Slides[0].Shapes[1] as ITable;
Step 3: Choose one table style from TableStylePreset and apply it to selected table.
table.StylePreset = TableStylePreset.MediumStyle2Accent2;
Step 4: Save the file.
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
Output:
Full Code:
using Spire.Presentation; namespace Apply_Built_in_Style { class Program { static void Main(string[] args) { Presentation ppt = new Presentation("test.pptx", FileFormat.Pptx2010); ITable table = ppt.Slides[0].Shapes[1] as ITable; table.StylePreset = TableStylePreset.MediumStyle2Accent2; ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010); } } }
Imports Spire.Presentation Namespace Apply_Built_in_Style Class Program Private Shared Sub Main(args As String()) Dim ppt As New Presentation("test.pptx", FileFormat.Pptx2010) Dim table As ITable = TryCast(ppt.Slides(0).Shapes(1), ITable) table.StylePreset = TableStylePreset.MediumStyle2Accent2 ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010) End Sub End Class End Namespace
How to fill the table cell with color in PowerPoint document in C#
A table provides a visual grouping of information and gives more convenience for writer to modify and query data in table. In particular when you have a table with colorful cells, your document would be more attractive. With the help of Spire.Presentation, developers can easily add tables and set table styles in PowerPoint document. This tutorial shows you how to fill the table cells with color in C#.
Step 1: Create a presentation document and load the file from disk.
Presentation presentation = new Presentation(); presentation.LoadFromFile("sample.pptx");
Step 2: Fill the table cell with color. You can fill all the cells or only fill one single row of cell in table with color.
foreach (TableRow row in table.TableRows) { foreach (Cell cell in row) { cell.FillFormat.FillType = FillFormatType.Solid; cell.FillFormat.SolidColor.Color = Color.Green; } }
Step 3: Save the presentation documents to file.
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
Effective screenshot for fill the color in all the table cells:
Effective screenshot for fill the color for the first row of table cell:
Full codes:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace colorfilltablecell { class Program { static void Main(string[] args) { Presentation presentation = new Presentation(); presentation.LoadFromFile("sample.pptx"); ITable table = null; foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable)shape; foreach (TableRow row in table.TableRows) { //TableRow row = table.TableRows[0]; foreach (Cell cell in row) { cell.FillFormat.FillType = FillFormatType.Solid; cell.FillFormat.SolidColor.Color = Color.Green; } } } } presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); } } }
Merge table cells on PowerPoint Slide
A table, as a great way to present data into groups, always plays an important role in any type of electronic documents. A well formatted table must contains larger or smaller cells without influencing the entire row or colum - and that's something that can be easily achieved by merging or splitting cells in your existing table.
In the next section, we will introduce you how to merge cells on en existing table which is embedded on a PowerPoint slide using Spire.Presentation for .NET. By convention, we need to download and install Spire.Presentation first, add its dll as a reference in you Visual C# or VB.NET project.
Assume you got a sample PPT file which contains a table like this, obviously you can merge cell 2 and cell 3 in the first column to display the account information more clearly.
How to achieve this programmatically using Spire.Presentation?
Step 1: Create a PPT document and load the sample file.
Presentation presentation = new Presentation(); presentation.LoadFromFile("table.pptx");
Step 2: Get the table and merge the second row and third row of the first column.
ITable table = null; foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable)shape; table.MergeCells(table[0, 1], table[0, 2], false); } }
Step 3: Save and launch the file.
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx");
Result:
Full code:
using Spire.Presentation; namespace MergeCells { class Program { static void Main(string[] args) { // create a PPT document and load file Presentation presentation = new Presentation(); presentation.LoadFromFile("table.pptx"); // get the table in PPT document ITable table = null; foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable)shape; //merge the second row and third row of the first column table.MergeCells(table[0, 1], table[0, 2], false); } } // save the document presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } } }
Imports Spire.Presentation Namespace MergeCells Class Program Private Shared Sub Main(args As String()) ' create a PPT document and load file Dim presentation As New Presentation() presentation.LoadFromFile("table.pptx") ' get the table in PPT document Dim table As ITable = Nothing For Each shape As IShape In presentation.Slides(0).Shapes If TypeOf shape Is ITable Then table = DirectCast(shape, ITable) 'merge the second row and third row of the first column table.MergeCells(table(0, 1), table(0, 2), False) End If Next ' save the document presentation.SaveToFile("result.pptx", FileFormat.Pptx2010) System.Diagnostics.Process.Start("result.pptx") End Sub End Class End Namespace
Remove Table from PowerPoint document
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 is a document in our website introducing you how to insert table. And in this document, I will introduce you how to remove tables within a PPT document.
Step 1: Create Presentation instance and load file.
Presentation presentation = new Presentation(); presentation.LoadFromFile("sample.ppt");
Step 2: Get the tables within the PPT document.
List shape_tems = new List(); foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { //add new table to table list shape_tems.Add(shape); } }
Step 3: Remove all tables.
foreach (IShape shape in shape_tems) { presentation.Slides[0].Shapes.Remove(shape); }
Step 4: Save the document.
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
Download and install Spire.Presentation for .NET and refer to below code to remove tables within PPT document.
Screenshots:
Before:
After:
Full Code:
//create Presentation instance and load file Presentation presentation = new Presentation(); presentation.LoadFromFile("sample.ppt"); //get the tables in PowerPoint document List shape_tems = new List(); foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { //add new table to table list shape_tems.Add(shape); } } //remove all tables foreach (IShape shape in shape_tems) { presentation.Slides[0].Shapes.Remove(shape); } //save the document presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx");
'create Presentation instance and load file Dim presentation As New Presentation() presentation.LoadFromFile("sample.ppt") 'get the tables in PowerPoint document Dim shape_tems As New List(Of IShape)() For Each shape As IShape In presentation.Slides(0).Shapes If TypeOf shape Is ITable Then 'add new table to table list shape_tems.Add(shape) End If Next 'remove all tables For Each shape As IShape In shape_tems presentation.Slides(0).Shapes.Remove(shape) Next 'save the document presentation.SaveToFile("result.pptx", FileFormat.Pptx2010) System.Diagnostics.Process.Start("result.pptx")
If you couldn't successfully use Spire.Presentation, please refer Spire.Presentation Quick Start which can guide you quickly use Spire.Presentation.
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:
using Spire.Presentation; using System; namespace Trasverse { class Program { static void Main(string[] args) { //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.
How to Remove Row or Column in Table
We have previously introduced how to insert a custom table and how to edit a table in PowerPoint documents. Now, we are going to make a brief introduction about how to remove the rows or columns that belong to an existing table.
Spire.Presentation for .NET, built to provide flexible PowerPoint document handling capabilities, allows developers to add a table to a slide and perform some basic operations as removing rows and columns in the table in an easy way.
Step 1: Create a PowerPoint instance and load a sample file.
Presentation presentation = new Presentation(); presentation.LoadFromFile("table.pptx");
Step 2: Find the table and remove the second column and second row.
ITable table = null; foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable)shape; table.ColumnsList.RemoveAt(1, false) table.TableRows.RemoveAt(1, false); } }
Step 3: Save the document.
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx");
The sample table with the second row and second column highlighted.
Result:
Full C# code:
using Spire.Presentation; namespace RemoveRow { class Program { static void Main(string[] args) { //create a PPT document Presentation presentation = new Presentation(); presentation.LoadFromFile("table.pptx"); //get the table in PPT document ITable table = null; foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable)shape; //remove the second column table.ColumnsList.RemoveAt(1, false); //remove the second row table.TableRows.RemoveAt(1, false); } } //save the document presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } } }
Edit Table in PowerPoint document
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 is a document in our website introducing you how to insert table. And in this document, you will be introduced how to edit a table within a PPT document.
Step 1: Create a Presentation instance and load the file.
Presentation presentation = new Presentation(); presentation.LoadFromFile("table.pptx");
Step 2: Store the data used in replacement in string [].
string[] str = new string[] { "Germany", "Berlin", "Europe", "0152458", "20860000" };
Step 3: Get the table within the PPT document.
ITable table = null; foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable) shape; } }
Step 4: Fill in the third row with new data and set the HighlightColor.
for (int i = 0; i < table.ColumnsList.Count;i++ ) { //replace the data in cell table[i, 2].TextFrame.Text = str[i]; //set the highlightcolor table[i, 2].TextFrame.TextRange.HighlightColor.Color = Color.BlueViolet; }
Step 5: Set the style of the table.
table.StylePreset = TableStylePreset.LightStyle1Accent2;
Step 6: Save the document.
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
Download and install Spire.Presentation for .NET and refer to below code to edit table within PPT document.
Screenshots and full code:
Before:
After:
using Spire.Presentation; using System.Drawing; namespace EditTable { class Program { static void Main(string[] args) { //create a PPT document Presentation presentation = new Presentation(); presentation.LoadFromFile("table.pptx"); //the data used in replacement string[] str = new string[] { "Germany", "Berlin", "Europe", "0152458", "20860000" }; ITable table = null; //get the table in PPT document foreach (IShape shape in presentation.Slides[0].Shapes) { if (shape is ITable) { table = (ITable)shape; //change the style of table table.StylePreset = TableStylePreset.LightStyle1Accent2; for (int i = 0; i < table.ColumnsList.Count; i++) { //replace the data in cell table[i, 2].TextFrame.Text = str[i]; //set the highlightcolor table[i, 2].TextFrame.TextRange.HighlightColor.Color = Color.BlueViolet; } } } //save the document presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } } }
Imports Spire.Presentation Imports System.Drawing Namespace EditTbale Class Program Private Shared Sub Main(args As String()) 'create a PPT document Dim presentation As New Presentation() presentation.LoadFromFile("table.pptx") 'the data used in replacement Dim str As String() = New String() {"Germany", "Berlin", "Europe", "0152458", "20860000"} Dim table As ITable = Nothing 'get the table in PPT document For Each shape As IShape In presentation.Slides(0).Shapes If TypeOf shape Is ITable Then table = DirectCast(shape, ITable) 'change the style of table table.StylePreset = TableStylePreset.LightStyle1Accent2 For i As Integer = 0 To table.ColumnsList.Count - 1 'replace the data in cell table(i, 2).TextFrame.Text = str(i) 'set the highlightcolor table(i, 2).TextFrame.TextRange.HighlightColor.Color = Color.BlueViolet Next End If Next 'save the document presentation.SaveToFile("result.pptx", FileFormat.Pptx2010) System.Diagnostics.Process.Start("result.pptx") End Sub End Class End Namespace
If you couldn't successfully use Spire.Presentation, please refer Spire.Presentation Quick Start which can guide you quickly use Spire.Presentation.
C#/VB.NET: Insert Tables in PowerPoint Presentations
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.