Skip to footer content
USING IRONXL

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 HtmlAgilityPack
Install-Package IronXL.Excel
Install-Package HtmlAgilityPack
SHELL

These 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.com
$vbLabelText   $csharpLabel

These 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.com
$vbLabelText   $csharpLabel

This 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.com
$vbLabelText   $csharpLabel

This 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.

How to Export an HTML Table to an Excel File in C#: Figure 1 - IronXL parsed table data output

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

How to Export an HTML Table to an Excel File in C#: Figure 2 - Parsed Excel data vs. the original HTML table

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.com
$vbLabelText   $csharpLabel

For 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.

Frequently Asked Questions

What is the primary benefit of using IronXL to convert HTML tables to Excel?

IronXL allows you to effortlessly convert HTML table data to Excel spreadsheets with a flexible approach that does not require Microsoft Office, enabling cross-platform compatibility.

Can IronXL handle complex HTML table structures when converting to Excel?

Yes, IronXL is designed to manage complex HTML table structures, ensuring that the data is accurately exported to Excel while maintaining the original layout and format.

Is it possible to automate the conversion of HTML tables to Excel using IronXL in C#?

Absolutely, IronXL supports automation, allowing developers to programmatically convert HTML tables to Excel sheets within C# applications, streamlining data processing workflows.

Does converting HTML tables to Excel using IronXL support different file formats?

IronXL supports various Excel file formats, including XLSX, XLS, and CSV, offering flexibility in choosing the output format that best suits your application's needs.

Do I need Microsoft Office installed to use IronXL for HTML to Excel conversion?

No, IronXL does not require Microsoft Office to be installed, providing a lightweight solution for converting HTML tables to Excel across different platforms.

What are the typical use cases for converting HTML tables to Excel using IronXL?

Common use cases include data migration, report generation, and further analysis of web page data, where exporting HTML table content to Excel is necessary for business applications.

How does IronXL compare to other libraries for HTML to Excel conversion?

While some libraries may offer built-in methods for HTML to Excel conversion, IronXL stands out by providing extensive features without the limitations often found in other solutions, such as restricted file format support.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More