How to Export an HTML Table to an Excel File in C#
Extracting HTML table data and converting it to an excel sheet is a common requirement in business applications, whether for data migration, report generation, or web pages that require further analyis. This article provides simple steps to export datatable content from HTML to Excel format. While some libraries offer built-in methods to convert HTML tables, these often come with limitations in file format or supporting features.
When you need to c export HTML table to excel, understanding the relationship between excel worksheet structures and HTML tables is crucial. This guide demonstrates how to efficiently transfer data from HTML tables to excel format, creating professional excel worksheet outputs that maintain data integrity.
IronXL provides a more flexible way to convert HTML table content into an Excel worksheet, combining its powerful Excel manipulation capabilities with HTML parsing to export HTML table to Excel in C#. Whether you need to download data from a url or process content from a database, this solution handles various input scenarios.
Why Choose IronXL to Export HTML Table Data?
IronXL excels at creating and manipulating Excel files without requiring Microsoft Office installation, making it ideal for server environments and cross-platform applications. When paired with HTML Agility Pack, a robust HTML file and content parser, IronXL becomes a versatile solution for converting any HTML table structure to excel sheet data. This approach works seamlessly with .NET applications and can handle large datasets efficiently.
Unlike libraries such as the Syncfusion Excel library's XlsIO, which offers an ImportHtmlTable function limited to specific HTML formats and table structures, the IronXL approach gives developers complete control over the parsing and conversion process. This flexibility means developers can handle complex scenarios like nested tables, custom data formatting, and selective column extraction that rigid built-in methods cannot accommodate. The default settings work well for most use cases, but you can customize every detail.
Additionally, IronXL provides comprehensive Excel features including formula support, cell styling, multiple worksheet management, and various export formats (XLSX, XLS, JSON, and CSV file). You can even create charts, export to pdf, and manage hidden field data, making it a complete solution for Excel automation needs beyond simple HTML table conversion. Whether working with a daatable object or a spradsheet file, IronXL handles the conversion seamlessly.
How to Set Up the Required Libraries?
First, install both IronXL and HTML Agility Pack through NuGet Package Manager. IronXL offers a free trial to test all features:
Install-Package IronXL.Excel
Install-Package HtmlAgilityPackInstall-Package IronXL.Excel
Install-Package HtmlAgilityPackThese NuGet packages allow you to create, load, and save Excel documents programmatically. Then import the necessary using statements to your C# code file:
using IronXL;
using HtmlAgilityPack;
using System;
using System.Linq;using IronXL;
using HtmlAgilityPack;
using System;
using System.Linq;IRON VB CONVERTER ERROR developers@ironsoftware.comThese libraries work seamlessly together, with HTML Agility Pack handling the HTML parsing while IronXL manages the Excel file creation and manipulation. This example demonstrates a straightforward approach to converting HTML tables to xlsx format.
How to Parse HTML Table Data with HTML Agility Pack?
HTML Agility Pack provides a simple way to navigate HTML documents using XPath expressions. The following code shows how to extract data from an HTML table and prepare it for export:
// Sample HTML table with product data
string htmlContent = @"
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>$999</td>
<td>15</td>
</tr>
<tr>
<td>Mouse</td>
<td>$25</td>
<td>50</td>
</tr>
<tr>
<td>Keyboard</td>
<td>$75</td>
<td>30</td>
</tr>
</tbody>
</table>";
// Load HTML document for parsing
var doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
// Select the HTML table element using XPath
var table = doc.DocumentNode.SelectSingleNode("//table");// Sample HTML table with product data
string htmlContent = @"
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Stock</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>$999</td>
<td>15</td>
</tr>
<tr>
<td>Mouse</td>
<td>$25</td>
<td>50</td>
</tr>
<tr>
<td>Keyboard</td>
<td>$75</td>
<td>30</td>
</tr>
</tbody>
</table>";
// Load HTML document for parsing
var doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
// Select the HTML table element using XPath
var table = doc.DocumentNode.SelectSingleNode("//table");IRON VB CONVERTER ERROR developers@ironsoftware.comThis code loads the HTML content into an HtmlDocument object and uses XPath to query and select the table element. The SelectSingleNode method returns the first table found in the HTML, making it easy to target specific tables when multiple exist. Each table row is processed to extract the cell value for conversion.
How to Export Parsed Data to Excel Using IronXL?
With IronXL, we can easily convert the parsed HTML table data into a professional Excel spreadsheet with proper formatting. The following code demonstrates how to export the data with custom font size and font family settings:
// Create a new Excel workbook
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet workSheet = workBook.CreateWorkSheet("Exported Data");
// Extract and write headers
var headers = table.SelectNodes(".//thead/tr/th");
if (headers != null)
{
for (int col = 0; col < headers.Count; col++)
{
workSheet.SetCellValue(0, col, headers[col].InnerText.Trim());
// Apply header formatting
var headerCell = workSheet.GetCellAt(0, col);
headerCell.Style.Font.Bold = true;
headerCell.Style.BackgroundColor = "#4CAF50";
}
}
// Extract and write data rows
var rows = table.SelectNodes(".//tbody/tr");
if (rows != null)
{
for (int row = 0; row < rows.Count; row++)
{
var cells = rows[row].SelectNodes("td");
if (cells != null)
{
for (int col = 0; col < cells.Count; col++)
{
string cellValue = cells[col].InnerText.Trim();
workSheet.SetCellValue(row + 1, col, cellValue);
}
}
}
}
// Auto-fit columns for better readability
for (int col = 0; col < headers?.Count; col++)
{
workSheet.AutoSizeColumn(col);
}
// Save the Excel file
workBook.SaveAs("ExportedTable.xlsx");// Create a new Excel workbook
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkSheet workSheet = workBook.CreateWorkSheet("Exported Data");
// Extract and write headers
var headers = table.SelectNodes(".//thead/tr/th");
if (headers != null)
{
for (int col = 0; col < headers.Count; col++)
{
workSheet.SetCellValue(0, col, headers[col].InnerText.Trim());
// Apply header formatting
var headerCell = workSheet.GetCellAt(0, col);
headerCell.Style.Font.Bold = true;
headerCell.Style.BackgroundColor = "#4CAF50";
}
}
// Extract and write data rows
var rows = table.SelectNodes(".//tbody/tr");
if (rows != null)
{
for (int row = 0; row < rows.Count; row++)
{
var cells = rows[row].SelectNodes("td");
if (cells != null)
{
for (int col = 0; col < cells.Count; col++)
{
string cellValue = cells[col].InnerText.Trim();
workSheet.SetCellValue(row + 1, col, cellValue);
}
}
}
}
// Auto-fit columns for better readability
for (int col = 0; col < headers?.Count; col++)
{
workSheet.AutoSizeColumn(col);
}
// Save the Excel file
workBook.SaveAs("ExportedTable.xlsx");IRON VB CONVERTER ERROR developers@ironsoftware.comThis code demonstrates IronXL's intuitive API for C# Excel manipulation. First, it creates a new WorkBook and WorkSheet. Then, it iterates through the parsed HTML table headers, placing them in the first row while applying bold formatting and a green background color. The data rows from the HTML table are processed similarly, with each cell's text content extracted and placed in the corresponding Excel cell. The AutoSizeColumn function ensures all content is visible, and finally, the workbook is saved as an XLSX file. You can easily download the generated file or store it in a database for later retrieval.

Here, you can see the original HTML table compared to the output from above:

Handling Common Scenarios
When working with multiple tables, simply use SelectNodes("//table") to get all tables and iterate through them, creating separate worksheets for each. This example shows how to handle complex scenarios with large datasets:
var tables = doc.DocumentNode.SelectNodes("//table");
foreach (var tbl in tables)
{
// Create new worksheet for each table
WorkSheet ws = workBook.CreateWorkSheet($"Table_{tables.IndexOf(tbl) + 1}");
// Process table as shown above
}var tables = doc.DocumentNode.SelectNodes("//table");
foreach (var tbl in tables)
{
// Create new worksheet for each table
WorkSheet ws = workBook.CreateWorkSheet($"Table_{tables.IndexOf(tbl) + 1}");
// Process table as shown above
}IRON VB CONVERTER ERROR developers@ironsoftware.comFor error handling, wrap the parsing logic in try-catch blocks to handle malformed HTML gracefully. IronXL automatically handles data type detection, converting numeric strings to numbers when appropriate. For more complex scenarios involving dynamic content, developers often combine this approach with tools like Selenium WebDriver for JavaScript-rendered tables.
When processing content from a url or database query for further analysis, you may need to handle additional details such as hidden field values or special formatting requirements. The default behavior works well for standard tables, but you can customize font size, font family, and other styling properties for each first column or any specific table row as needed.
Conclusion
The combination of IronXL and HTML Agility Pack provides a robust, flexible solution for exporting HTML tables to Excel in C#. This article has demonstrated simple steps to convert HTML content to xlsx format, export datatable information, and create professional spradsheet files. This approach offers more control than rigid built-in methods, allowing developers to handle complex HTML structures while leveraging IronXL's comprehensive Excel features.
Whether you're building web scrapers, migrating legacy data from a database, automating report generation with charts, or performing data analyis on large datasets, this solution scales to meet enterprise needs. The code examples provided show how to handle various input sources, from static HTML to dynamic content retrieved via url. You can easily export the results for download or further processing in your .NET application.
Ready to transform your HTML data into professional excel sheet files? Start your free trial of IronXL today and experience the flexibility of programmatic Excel manipulation without Office dependencies. For production deployments, explore our licensing options starting at $799.







