Many new features have been added with the release of GrapeCity Documents for Excel v3.2. Generate secure Excel reports through digital signatures, enhanced Excel templates with tables, sparklines, and more. GcExcel v3.2 supports Excel to HTML conversion. We highlight this feature below. The Excel to HTML feature converts Excel reports to HTML format, and preserves Excel formats during the conversion.

In this article, we'll walk through the feature of exporting Excel report to HTML and learn which components of the Excel report can be converted.

Additionally, we will explore the HtmlSaveOptions class and use it to export an Excel report to HTML format.

Export Excel Spreadsheet and Excel Ranges to HTML

The above image is a snapshot of an Excel report converted into HTML format. We outline this process below.

Export Excel Files to HTML

The Export Excel to HTML feature in GcExcel v3.2 converts Excel reports to HTML format. It gives users flexibility as it supports exporting workbooks, worksheets, or any specific range to HTML. GcExcel v3.2 helps businesses convert complex spreadsheets into essay-to-read reports, for display on their website.

GcExcel v3.2 offers two ways to export Excel files to HTML:

1. Using SaveFileFormat Enum:

The extended SaveFileFormat enumeration gives users the ability to export Excel files to HTML. In the v3.2 release, SaveFileFormat enumeration includes the HTML option, along with .csv, PDF, .xls and, .xlsx options.

Convert Workbooks, worksheets and ranges to HTML using the IWorkbook interface, Save method, and extended SaveFileFormat enum:

workbook.Save(outputStream, SaveFileFormat.Html); 

2. Using HtmlSaveOptions Class:

HtmlSaveOptions is a new class added to the GcExcel API. It specifies the options for saving Excel reports as HTML files. The class supports many useful properties such as ExportCssSeparately, ExportFileName, and ExportHeadings. These properties preserve the Excel file’s settings when it's converted to HTML format.

Include grid lines and headers by setting the ExportGridlines and ExportHeadings properties.

HtmlSaveOptions options = new HtmlSaveOptions();
options.ExportFileName = "Analysis";
options.ExportHeadings = true;
options.ExportGridlines = true;
workbook.Save(outputStream, options);

Export Excel Spreadsheet and Excel Ranges to HTML

The above image shows how defining the HtmlSaveOptions properties preserves the original Excel settings.

While converting the Excel reports to HTML, the HTML files are generated within the .zip directory and all other components like CSS or charts present in the Excel report are contained in a separate folder inside .zip.

Generate the .HTML file directly without creating a .zip or separate file by setting the HtmlSaveOptions's ExportSingleTab, ExportCssSeparately properties to false and the ExportImageAsBase64 property to true.

Below we outline the processes for exporting Excel reports, spreadsheets, and partial spreadsheets to HTML.

Export Excel Workbooks to HTML

Converting a full Excel report is termed as Exporting a workbook. Ideal for scenarios in which an organization has multiple sheets of Excel reports and all of them are necessary to present a complete online summary.

Upon completion of conversion, a zip file is created. The .zip file includes:

  1. An .HTML file for the exported workbook
  2. A folder that contains .htm files for all WorkSheets
  3. A .css file used for styling the HTML files
  4. .png files files for each element present in the workbook such as charts and images
//create a zip file stream 
FileStream outputStream = new FileStream("saveworkbooktohtml.zip", FileMode.Create); 

//create a new workbook 
var workbook = new GrapeCity.Documents.Excel.Workbook(); 

//load the resource in Stream based on export type 
var assembly = typeof(HtmlExporting_Demo.Program).GetTypeInfo().Assembly; 
Stream fileStream = assembly.GetManifestResourceStream("ProjectTrackerXlsx"); 
//open XLSX file in GcExcel 
workbook.Open(fileStream); 

//export workbook to Html format 
workbook.Save(outputStream, SaveFileFormat.Html); 

Export Excel Spreadsheet and Excel Ranges to HTML

The above image shows what an exported workbook looks like an HTML format. Note the multiple sheets of the workbook exported as separate tabs at the bottom of the exported HTML file.

Export Worksheet

Converting a specific spreadsheet from a workbook is called Exporting a Worksheet. Export any sheet within the Excel report to HTML with this option. Exporting a specific worksheet is helpful in scenarios where the user wants to present a summary of a specific worksheet on a dedicated web-page.

Upon conversion, a zip file is created. The .zip file includes:

  1. An .HTML file for the exported worksheet and a folder that contains a .htm file for the worksheet
  2. A .css file used for styling the HTML file
  3. .png files for each element present in the in worksheet such as charts and images
//create a zip file stream 
FileStream outputStream = new FileStream("saveworkksheettohtml.zip", FileMode.Create); 

//create a new workbook 
var workbook = new GrapeCity.Documents.Excel.Workbook(); 

//load the resource in Stream based on export type 
var assembly = typeof(HtmlExporting_Demo.Program).GetTypeInfo().Assembly; 
Stream fileStream = assembly.GetManifestResourceStream("BreakEvenXlsx"); 

//open XLSX file in GcExcel 
workbook.Open(fileStream); 

HtmlSaveOptions options = new HtmlSaveOptions(); 

//set exported html file name 
options.ExportFileName = "Analysis";                     

//export worksheet to Html format 
workbook.Save(outputStream, options);

Export Excel Spreadsheet and Excel Ranges to HTML

The above image shows a specific worksheet exported to HTML format using GcExcel v3.2.

Export Range

Converting a specific cell range of a specific worksheet in a workbook is referred to as Exporting a Range. Export any cell range within any sheet of an Excel report to HTML format with this option. Helpful in scenarios where an organization wants to present a specific range of a summary on their website.

Upon conversion, a zip file is created. The content of the .zip file includes:

  1. An .HTML file for the exported range and a folder that contains a .htm file for the range
  2. A .css file used for styling the HTML file
  3. .png files files for each element present in the in range
//create a zip file stream 
FileStream outputStream = new FileStream("saverangetohtml.zip", FileMode.Create); 

//create a new workbook 
var workbook = new GrapeCity.Documents.Excel.Workbook(); 

//load the resource in Stream based on export type 
var assembly = typeof(HtmlExporting_Demo.Program).GetTypeInfo().Assembly; 
Stream fileStream = assembly.GetManifestResourceStream("BreakEvenXlsx"); 
//open XLSX file in GcExcel 
workbook.Open(fileStream); 

HtmlSaveOptions options = new HtmlSaveOptions(); 
//set export area 
options.ExportArea = "A2:G23"; 

//set exported html file name 
options.ExportFileName = "Range"; 

//export sheet table range to Html format 
workbook.Save(outputStream, options); 

Export Excel Spreadsheet and Excel Ranges to HTML

The above image shows a specific range in an Excel file containing a table exported to HTML.

Export SpreadSheet to Single HTML

In most cases, the workbook is exported to a zip stream containing the main HTML file. A folder contains the other files (htm files for all worksheets, and CSS files used for styling the HTML files).

With the HtmlSaveOptions class converting the Excel report to a single HTML file does not include a zip file containing separate files for each sheet and CSS.

To export an Excel report as a single HTML file, check for the following:

//create a zip file stream
FileStream outputStream = new FileStream("saveworksheettosinglehtml.html", FileMode.Create);

//create a new workbook
var workbook = new GrapeCity.Documents.Excel.Workbook();

//load the resource in Stream based on export type
var assembly = typeof(HtmlExporting_Demo.Program).GetTypeInfo().Assembly;
Stream fileStream = assembly.GetManifestResourceStream("BreakEvenXlsx");

//open XLSX file in GcExcel
workbook.Open(fileStream);

HtmlSaveOptions options = new HtmlSaveOptions();
//export first sheet
options.ExportSheetName = workbook.Worksheets[0].Name;
// Set exported image as base64
options.ExportImageAsBase64 = true;

// Set exported css style in html file
options.ExportCssSeparately = false;

// Set not to export single tab in html
options.ExportSingleTab = false;

workbook.Save(outputStream, options);

Converting Excel Reports to HTML

Now, that we know about the export Excel to HTML feature, let's build a Visual Studio application to export an Excel report to HTML format.

Step 1. Installation

  1. In you .NET Core Console application, right-click ‘Dependencies’ and select ‘Manage NuGet Packages’
  2. Under the ‘Browse’ tab search for ‘GrapeCity.Documents.Excel’ and click Install
  3. While installing, you’ll receive two confirmation dialogs: ‘Preview Changes’ and ‘License Acceptance’, click ‘Ok’ and ‘I Agree’ respectively
  4. Also, add reference to 'GrapeCity.Documents.DataVisualization.Chart', 'GrapeCity.Documents.Pdf', and 'System.IO.Packaging' packages

Step 2. C# Project Setup

Add namespace

In the Program file, import the following namespace:

using GrapeCity.Documents.Excel;

Create a new workbook

In the main function, add the following code to create a new GcExcel workbook.

Workbook workbook = new Workbook();

Load Excel workbook

In the main function, add the following code to load Excel report as file stream from project's Resources.

var assembly = typeof(HtmlExporting_Demo.Program).GetTypeInfo().Assembly;
Stream fileStream = assembly.GetManifestResourceStream("ProjectTrackerXlsx");
Open loaded FileStream in workbook

In the main function, invoke the workbook's Open method to open the Excel report in GcExcel.

workbook.Open(fileStream);

Export loaded Excel to HTML

In the main function, invoke the workbook's Save method with HTML option of SaveFileFormat enumeration.

workbook.Save(outputStream, SaveFileFormat.Html);

Invoking the Save method converts the Excel report to HTML.

Note:

  1. While using unlicensed/evaluation version of GcExcel
    • An "evaluation warning" sheet is appended to a workbook when exporting an Excel file
    • An "evaluation warning" message is added to the head of the HTML file in each worksheet
  2. GcExcel for JAVA cannot export charts to HTML, it's currently a limitation

.NET Help | JAVA Help | .NET Demo | JAVA Demo

What do you think about the new features? Share your comments below.

Try GcExcel free for 30 days

Download the latest version of GrapeCity Documents for Excel, .NET

Download Now!

Try GcExcel Java free for 30 days

Download the latest version of GrapeCity Documents for Excel, Java Edition

Download Now!