Build an HTML Table with a .NET API
Aspose.HTML for .NET
provides table-specific DOM classes for creating structured HTML in C#. Start with an
HTMLDocument
, create an
HTMLTableElement
, insert rows and cells, and append the completed table to the document before saving it.
Create a Styled HTML Table in C#
The following example programmatically creates a table with three rows and two columns, applies CSS borders and cell padding, and saves the completed document as table.html.
C# Code to Create an HTML Table
using Aspose.Html;
using HTMLDocument document = new HTMLDocument();
// Create and style the table
HTMLTableElement table = (HTMLTableElement)document.CreateElement("table");
table.Style.SetProperty("border-collapse", "collapse");
table.Style.SetProperty("width", "100%");
// Add rows and cells through the table DOM API
for (int rowIndex = 0; rowIndex < 3; rowIndex++)
{
HTMLTableRowElement row = (HTMLTableRowElement)table.InsertRow(table.Rows.Length);
for (int columnIndex = 0; columnIndex < 2; columnIndex++)
{
HTMLTableCellElement cell = (HTMLTableCellElement)row.InsertCell(row.Cells.Length);
cell.TextContent = $"Row {rowIndex + 1}, Column {columnIndex + 1}";
cell.Style.SetProperty("border", "1px solid #808080");
cell.Style.SetProperty("padding", "8px");
}
}
document.Body.AppendChild(table);
document.Save("table.html");
Steps to Create an HTML Table in C#
- Create a new document with an
HTMLDocumentconstructor, or load an existing HTML file when the table must be added to current content. - Create a
<table>element withCreateElement()and cast it toHTMLTableElement. - Add rows with
InsertRow()and add data cells withInsertCell(). - Set each cell’s
TextContent, apply CSS styles, and append the completed table to the document body. - Call
Save()to write the result totable.html.
HTML Table Creation Options
- Create semantic table sections. Build separate
<thead>,<tbody>, and<tfoot>elements when headers, data, and summary rows have different roles. - Control table placement. Append the table to
document.Bodyor select a specific container before inserting it into an existing document. - Style the result with CSS. Set borders, spacing, widths, alignment, colors, and responsive layout through the element’s
Styledeclaration or a stylesheet. - Add identifiers and metadata. Assign IDs, classes,
data-*attributes, and other DOM attributes when the table must be selected or processed later. - Continue with a related workflow. Use Edit HTML Table for existing content, Extract Table Data for structured values, or HTML Table Generator for an interactive table builder.
HTML Table and DOM Documentation
See Edit an HTML Document for the general DOM workflow used to create, insert, update, and remove HTML elements. The API Reference documents the table-specific classes for tables, sections, rows, and cells as well as the DOM methods used to attach them to a document.
Get Started with Aspose.HTML for .NET API
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
HTML Table Creation FAQ
1. Why should table headers use <th> instead of <td>?
The <th> element identifies a header cell, while <td> represents ordinary table data. Header cells give the table clearer structure and allow browsers and assistive technology to associate labels with related data.
2. Can I add a new table to an existing HTML file?
Yes. Initialize HTMLDocument with the path to the existing file, create the table, append it to the document body or another selected container, and save the result to a new or existing HTML file.
3. Can table rows be generated from a C# collection?
Yes. Iterate through the collection, insert one HTMLTableRowElement for each record, and create the required cells from its values. The API does not require the data to come from a particular source.
4. Should I use the border, align, and width HTML attributes?
Prefer CSS for presentation. Set border, width, margins, and alignment through Style or a stylesheet so the generated table does not depend on obsolete presentational HTML attributes.
5. How do I make one cell span several rows or columns?
Set RowSpan or ColSpan on the corresponding HTMLTableCellElement. The values determine how many rows or columns the cell occupies in the table structure.
Related HTML Table Workflows
Extract, edit, or process HTML tables with focused Aspose.HTML for .NET guides.