A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(http://103.13.28.148/cstovb/cstovb/): failed to open stream: HTTP request failed!

Filename: libraries/Converter.php

Line Number: 134

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 134
Function: file_get_contents

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior

Filename: libraries/Converter.php

Line Number: 136

Backtrace:

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 136
Function: strpos

File: /var/www/ironpdf.com/application/libraries/Converter.php
Line: 92
Function: convert_cs_to_vb_with_vbconverter

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 1814
Function: convert_cs_to_vb

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 559
Function: render_cs_window

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 532
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 561
Function: {closure}

File: /var/www/ironpdf.com/application/libraries/MarkdownProcessor.php
Line: 28
Function: render_language_type

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 340
Function: processMarkdownViewContent

File: /var/www/ironpdf.com/application/controllers/Products/Blog.php
Line: 77
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

Convert HTML Tables to Excel in C# (Developer Guide)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.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to ...Read More
Ready to Get Started?
Nuget Downloads 1,648,013 | Version: 2025.10 just released