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

GemBox.Spreadsheet allows you to save an Excel workbook or worksheet in HTML or MHTML format.

The following example shows how you can convert an Excel file to HTML in C# and VB.NET.

HTML save options

You can use the HtmlSaveOptions class to set options such as:

  • Embed images directly within the HTML file.
  • Export the whole Excel file, or only the selected sheet.
  • 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 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 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 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 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())
            {
                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 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()
                writer.WriteElementString("h1", worksheet.Name)

                workbook.Worksheets.ActiveWorksheet = worksheet
                workbook.Save(writer, options)
            Next

            writer.WriteEndDocument()
        End Using
    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.