Table (19)
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.
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.
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"); } } }
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.
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.