C#: Convert HTML to Excel

HTML (HyperText Markup Language) is primarily used for structuring content on web pages. While it excels at presenting information visually on the web, it lacks the robust analytical capabilities and data manipulation features found in spreadsheet software like Excel. By converting HTML data to Excel, users can leverage Excel's advanced functionalities like formulas, charts, tables, and macros to organize and analyze data efficiently. In this article, we will explain how to convert HTML to Excel in C# using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Convert HTML to Excel in C#

Spire.XLS for .NET offers the Workbook.LoadFromHtml() method to load an HTML file. After loading the HTML file, you can easily save it in Excel format using the Workbook.SaveToFile() method. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an HTML file using the Workbook.LoadFromHtml() method.
  • Save the HTML file in Excel format using the Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

namespace ConvertHtmlToExcel
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Specify the input HTML file path
            string filePath = @"C:\Users\Administrator\Desktop\Sample.html";

            // Create an object of the workbook class
            Workbook workbook = new Workbook();

            // Load the HTML file
            workbook.LoadFromHtml(filePath);

            // Save the HTML file in Excel XLSX format
            string result = @"C:\Users\Administrator\Desktop\ToExcel.xlsx";
            workbook.SaveToFile(result, ExcelVersion.Version2013);

            workbook.Dispose();
        }
    }
}

C#: Convert HTML to Excel

Insert HTML String to Excel in C#

In addition to converting HTML files to Excel, Spire.XLS for .NET also allows you to insert HTML strings into Excel cells by using the CellRange.HtmlString property. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Get a specific worksheet by its index (0-based) using the Workbook.Worksheets[index] property.
  • Get the cell that you want to add an HTML string to using the Worksheet.Range[] property.
  • Add an HTML sting to the cell using the CellRange.HtmlString property.
  • Save the resulting workbook to a new file using the Workbook.SaveToFile() method.
  • C#
using Spire.Xls;

namespace InsertHtmlStringInExcel
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create an object of the workbook class
            Workbook workbook = new Workbook();
            // Get the first sheet
            Worksheet sheet = workbook.Worksheets[0];

            // Specify the HTML string
            string htmlCode = "<p><font size='12'>This is a <b>paragraph</b> with <span style='color: red;'>colored text</span>.</font></p>";

            // Get the cell that you want to add the HTML string to
            CellRange range = sheet.Range["A1"];
            // Add the HTML string to the cell
            range.HtmlString = htmlCode;

            // Auto-adjust the width of the first column based on its content
            sheet.AutoFitColumn(1);

            // Save the resulting workbook to a new file
            string result = @"C:\Users\Administrator\Desktop\InsertHtmlStringIntoCell.xlsx";
            workbook.SaveToFile(result, ExcelVersion.Version2013);

            workbook.Dispose();
        }
    }
}

C#: Convert HTML to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.