How to Work with Tables in .NET
This guide shows how to create tables on PowerPoint slides using Aspose.Slides FOSS for .NET. Call slide.Shapes.AddTable(x, y, columnWidths, rowHeights) to add a table, access cells via table.Rows[row][col], and apply text formatting through PortionFormat on each cell.
Step-by-Step Guide
Step 1: Install the Package
Add the following package reference to your project by running the dotnet CLI install command:
dotnet add package Aspose.Slides.FossStep 2: Create or Open a Presentation
Use a using statement to construct a Presentation, access the first slide via prs.Slides[0], then save when done:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
// ... add table ...
prs.Save("table.pptx", SaveFormat.Pptx);Step 3: Define Column Widths and Row Heights
Tables require explicit column widths and row heights in points (1 point = 1/72 inch). A standard slide is 720 points wide and 540 points tall.
var colWidths = new double[] { 200.0, 150.0, 150.0 }; // 3 columns
var rowHeights = new double[] { 45.0, 40.0, 40.0 }; // 3 rowsStep 4: Add the Table
slide.Shapes.AddTable(x, y, columnWidths, rowHeights) creates the table at position (x, y):
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var colWidths = new double[] { 200.0, 150.0, 150.0 };
var rowHeights = new double[] { 45.0, 40.0, 40.0 };
var table = slide.Shapes.AddTable(50, 100, colWidths, rowHeights);
prs.Save("table.pptx", SaveFormat.Pptx);Step 5: Set Cell Text
Access cells via table.Rows[rowIndex][colIndex] and assign text through .TextFrame.Text. Row and column indices are zero-based, so the header row is index 0 and data rows start at index 1:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;
using var prs = new Presentation();
var slide = prs.Slides[0];
var colWidths = new double[] { 200.0, 150.0, 150.0 };
var rowHeights = new double[] { 45.0, 40.0, 40.0 };
var table = slide.Shapes.AddTable(50, 100, colWidths, rowHeights);
// Header row (row 0)
string[] headers = { "Product", "Units Sold", "Revenue" };
for (int col = 0; col < headers.Length; col++)
table.Rows[0][col].TextFrame.Text = headers[col];
// Data rows
string[][] data = {
new[] { "Widget A", "1,200", "$24,000" },
new[] { "Widget B", "850", "$17,000" },
};
for (int rowIdx = 0; rowIdx < data.Length; rowIdx++)
for (int col = 0; col < data[rowIdx].Length; col++)
table.Rows[rowIdx + 1][col].TextFrame.Text = data[rowIdx][col];
prs.Save("sales-table.pptx", SaveFormat.Pptx);Step 6: Format Header Cell Text
Apply bold font formatting to header cells by accessing the first paragraph’s PortionFormat via cell.TextFrame.Paragraphs[0].Portions[0].PortionFormat and setting fmt.FontBold = NullableBool.True:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
for (int col = 0; col < headers.Length; col++)
{
var cell = table.Rows[0][col];
var portions = cell.TextFrame.Paragraphs[0].Portions;
if (portions.Count > 0)
{
var fmt = portions[0].PortionFormat;
fmt.FontBold = NullableBool.True;
fmt.FillFormat.FillType = FillType.Solid;
fmt.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 255, 255, 255);
}
}Complete Working Example
The following self-contained script creates a regional revenue table with a bold header row and four data rows, then saves the result as a PPTX file:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;
string[][] dataRows = {
new[] { "North", "$1.2M", "+8%" },
new[] { "South", "$0.9M", "+4%" },
new[] { "East", "$1.5M", "+12%" },
new[] { "West", "$0.7M", "+2%" },
};
string[] headers = { "Region", "Revenue", "Growth" };
using var prs = new Presentation();
var slide = prs.Slides[0];
var colWidths = new double[] { 180.0, 140.0, 120.0 };
var rowHeights = new double[dataRows.Length + 1];
rowHeights[0] = 45.0;
for (int i = 1; i < rowHeights.Length; i++) rowHeights[i] = 38.0;
var table = slide.Shapes.AddTable(60, 80, colWidths, rowHeights);
// Header row
for (int col = 0; col < headers.Length; col++)
{
var cell = table.Rows[0][col];
cell.TextFrame.Text = headers[col];
if (cell.TextFrame.Paragraphs[0].Portions.Count > 0)
{
var fmt = cell.TextFrame.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontBold = NullableBool.True;
}
}
// Data rows
for (int rowIdx = 0; rowIdx < dataRows.Length; rowIdx++)
for (int col = 0; col < dataRows[rowIdx].Length; col++)
table.Rows[rowIdx + 1][col].TextFrame.Text = dataRows[rowIdx][col];
prs.Save("regional-revenue.pptx", SaveFormat.Pptx);
Console.WriteLine("Saved regional-revenue.pptx");Common Issues and Fixes
IndexOutOfRangeException when accessing table.Rows[row][col]
Row and column indices are zero-based. If you defined rowHeights with 3 elements, valid row indices are 0, 1, 2.
Cell text does not appear in the saved file
Always assign through .TextFrame.Text, not through .Text directly on the cell object. Accessing .Text on a table cell does not compile or silently fails.
Table position is off the slide
Check that x + sum(colWidths) <= 720 and y + sum(rowHeights) <= 540 for a standard slide.
Frequently Asked Questions
Can I merge table cells?
Yes. Use Table.MergeCells(ICell cell1, ICell cell2, bool allowSplitting) to merge two adjacent cells. Set allowSplitting to false to prevent the merged cell from being split again. Example:
// Merge cell (row 0, col 0) with cell (row 0, col 1)
var cell1 = table.Rows[0][0];
var cell2 = table.Rows[0][1];
table.MergeCells(cell1, cell2, false);Can I apply a table-wide background color?
Apply fill formatting to each individual cell:
using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
for (int row = 0; row < table.Rows.Count; row++)
for (int col = 0; col < table.Rows[row].Count; col++)
{
var cell = table.Rows[row][col];
cell.CellFormat.FillFormat.FillType = FillType.Solid;
cell.CellFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 240, 248, 255);
}Can I set cell border styles?
Cell border properties are accessible through cell.CellFormat.BorderLeft, BorderTop, BorderRight, and BorderBottom properties. Refer to the API reference for the full list of border format attributes.