Convert Excel 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 sample shows how to convert an Excel file to HTML in C# and VB.NET.

Note the use of HtmlSaveOptions to set the worksheet options that are supported when saving an Excel file in HTML format. The sample demonstrates saving of an entire Excel workbook and all its images in HTML format.

Since worksheet files must be physically saved on a disk, the following sample cannot be run interactively from the Sample Explorer.

Input file: HtmlExport.xlsx.

Screenshot

Html Export Screenshot

See the full code below.

1using System;
2using System.Collections.Generic;
3using System.Text;
4using GemBox.Spreadsheet;
5using GemBox.Spreadsheet.ConditionalFormatting;
6using GemBox.Spreadsheet.PivotTables;
7
8class Sample
9{
10    [STAThread]
11    static void Main(string[] args)
12    {
13        // If using Professional version, put your serial key below.
14        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
15
16        ExcelFile ef = ExcelFile.Load("HtmlExport.xlsx");
17
18        var ws = ef.Worksheets[0];
19
20        // Some of the properties from ExcelPrintOptions class are supported in HTML export.
21        ws.PrintOptions.PrintHeadings = true;
22        ws.PrintOptions.PrintGridlines = true;
23
24        // Print area can be used to specify custom cell range which should be exported to HTML.
25        ws.NamedRanges.SetPrintArea(ws.Cells.GetSubrange("A1", "I42"));
26
27        HtmlSaveOptions options = new HtmlSaveOptions()
28        {
29            HtmlType = HtmlType.Html,
30            SelectionType = SelectionType.EntireFile
31        };
32
33        ef.Save("HtmlExport.html", options);
34    }
35}
1Imports System
2Imports System.Collections.Generic
3Imports System.Text
4Imports GemBox.Spreadsheet
5Imports GemBox.Spreadsheet.ConditionalFormatting
6Imports GemBox.Spreadsheet.PivotTables
7
8Module Samples
9
10    Sub Main()
11
12        ' If using Professional version, put your serial key below.
13        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
14
15        Dim ef = ExcelFile.Load("HtmlExport.xlsx")
16
17        Dim ws = ef.Worksheets(0)
18
19        ' Some of the properties from ExcelPrintOptions class are supported in HTML export.
20        ws.PrintOptions.PrintHeadings = True
21        ws.PrintOptions.PrintGridlines = True
22
23        ' Print area can be used to specify custom cell range which should be exported to HTML.
24        ws.NamedRanges.SetPrintArea(ws.Cells.GetSubrange("A1", "I42"))
25
26        Dim options = New HtmlSaveOptions() With { _
27                .HtmlType = HtmlType.Html, _
28                .SelectionType = SelectionType.EntireFile _
29            }
30
31        ef.Save("HtmlExport.html", options)
32
33    End Sub
34
35End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.