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. You can use the 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. By default If you want to export the whole Excel file together with images, shapes, textboxes, and charts, into a single HTML page, you can use Alternatively, you can export each sheet as an HTML table by using HTML save options
HtmlSaveOptions
class to set options such as: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
HtmlSaveOptions.HtmlType
is set to HtmlType.Html
, which will export the entire spreadsheet content into several files.HtmlType.Mhtml
which will export the entire spreadsheet content as a MIME HTML (MHTML) file.HtmlType.HtmlTable
and combine them into a single HTML content, as shown in the next example.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())
{
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()
writer.WriteElementString("h1", worksheet.Name)
workbook.Worksheets.ActiveWorksheet = worksheet
workbook.Save(writer, options)
Next
writer.WriteEndDocument()
End Using
End Sub
End Module