Convert between Excel and HTML files in C# and VB.NET

With GemBox.Spreadsheet you can achieve quick and efficient conversion between Excel workbooks and HTML pages, using simple and straightforward C# or VB.NET code.

Convert an Excel file to HTML pages

With GemBox.Spreadsheet you can save an Excel files in an HTML or MHTML format. You can use the HtmlSaveOptions class to set options such as:

  • Embed images directly within the HTML file.
  • Export the whole workbook, or only the selected worksheet.
  • Export part of the file as a HTML table that can be inserted into your HTML page.

Properties specified on the Excel file level, such as print headings, print gridlines, and print area, are also supported.

The following example shows how you can save an entire Excel workbook and all its images in HTML format. Since worksheet files must be physically saved on a disk, the following example cannot be run interactively from the Example Explorer.

Converting Excel file to HTML content in C# and VB.NET
Screenshot of a converted Excel file to HTML pages
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = ExcelFile.Load("%#HtmlExport.xlsx%");
        var worksheet = workbook.Worksheets[0];

        // Set some ExcelPrintOptions properties for HTML export.
        worksheet.PrintOptions.PrintHeadings = true;
        worksheet.PrintOptions.PrintGridlines = true;

        // Specify cell range which should be exported to HTML.
        worksheet.NamedRanges.SetPrintArea(worksheet.Cells.GetSubrange("A1", "J42"));

        var options = new HtmlSaveOptions()
        {
            HtmlType = HtmlType.Html,
            SelectionType = SelectionType.EntireFile
        };

        workbook.Save("HtmlExport.html", options);
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = ExcelFile.Load("%#HtmlExport.xlsx%")
        Dim worksheet = workbook.Worksheets(0)

        ' Set some ExcelPrintOptions properties for HTML export.
        worksheet.PrintOptions.PrintHeadings = True
        worksheet.PrintOptions.PrintGridlines = True

        ' Specify cell range which should be exported to HTML.
        worksheet.NamedRanges.SetPrintArea(worksheet.Cells.GetSubrange("A1", "J42"))

        Dim options = New HtmlSaveOptions() With
        {
            .HtmlType = HtmlType.Html,
            .SelectionType = SelectionType.EntireFile
        }

        workbook.Save("HtmlExport.html", options)
    End Sub
End Module

Convert multiple Excel sheets to a single HTML page

By default HtmlSaveOptions.HtmlType is set to HtmlType.Html, which will export the entire spreadsheet content into several files.

If you want to export the whole Excel file together with images, shapes, textboxes, and charts, into a single HTML page, you can use HtmlType.Mhtml which will export the entire spreadsheet content as a MIME HTML (MHTML) file.

Alternatively, you can export each sheet as an HTML table by using HtmlType.HtmlTable and combine them into a single HTML content, as shown in the next example.

Converting Excel sheets to single HTML content by exporting each sheet as separate HTML table in C# and VB.NET
Screenshot of converted Excel sheets to a HTML page
Upload your file (Drag file here)
using System.Linq;
using System.Xml;
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = ExcelFile.Load("%InputFileName%");

        // Specify exporting of Excel data as an HTML table with embedded images.
        var options = new HtmlSaveOptions()
        {
            EmbedImages = true,
            HtmlType = HtmlType.HtmlTable
        };

        using (var writer = XmlWriter.Create("SingleHtmlExport.html",
            new XmlWriterSettings() { OmitXmlDeclaration = true }))
        {
            writer.WriteStartElement("html");
            writer.WriteStartElement("body");

            // Write Excel sheets to a single HTML file in reverse order.
            foreach (var worksheet in workbook.Worksheets.Reverse())
            {
                if (worksheet.Visibility != SheetVisibility.Visible)
                    continue;

                writer.WriteElementString("h1", worksheet.Name);
                workbook.Worksheets.ActiveWorksheet = worksheet;
                workbook.Save(writer, options);
            }

            writer.WriteEndDocument();
        }
    }
}
Imports System.Linq
Imports System.Xml
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = ExcelFile.Load("%InputFileName%")

        ' Specify exporting of Excel data as an HTML table with embedded images.
        Dim options As New HtmlSaveOptions() With
        {
            .EmbedImages = True,
            .HtmlType = HtmlType.HtmlTable
        }

        Using writer = XmlWriter.Create("SingleHtmlExport.html",
            New XmlWriterSettings() With {.OmitXmlDeclaration = True})

            writer.WriteStartElement("html")
            writer.WriteStartElement("body")

            ' Write Excel sheets to a single HTML file in reverse order.
            For Each worksheet In workbook.Worksheets.Reverse()

                If worksheet.Visibility <> SheetVisibility.Visible Then Continue For

                writer.WriteElementString("h1", worksheet.Name)
                workbook.Worksheets.ActiveWorksheet = worksheet
                workbook.Save(writer, options)

            Next

            writer.WriteEndDocument()
        End Using
    End Sub
End Module

Convert an HTML page to an Excel file

You can import an HTML file as a single Excel sheet by using one of the ExcelFile.Load overload methods. Also, your can import HTML text into a single cell by using the ExcelCell.SetValue(String, HtmlLoadOptions) method.

The following example shows how you can convert an HTML file to an Excel workbook.

Converting HTML content to Excel sheet in C# and VB.NET
Screenshot of a converted HTML page to Excel file
Upload your file (Drag file here)
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input HTML file.
        var workbook = ExcelFile.Load("%InputFileName%");

        // Save output XLSX file.
        workbook.Save("HtmlImport.xlsx");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load input HTML file.
        Dim workbook = ExcelFile.Load("%InputFileName%")

        ' Save output XLSX file.
        workbook.Save("HtmlImport.xlsx")

    End Sub
End Module

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy