Export Excel Spreadsheets and Excel Ranges to HTML
GrapeCity is excited to announce the release of GrapeCity Documents for Excel API (GcExcel) v3.2.
This release delivers many new features. Generate secure Excel reports through digital signatures, create enhanced Excel templates with tables, utilize sparklines, and more. Additionally, GcExcel v3.2 now supports Excel to HTML conversion. We highlight this feature below. The Excel to HTML feature converts Excel reports to HTML and preserves Excel formats during the conversion.
The above image is a snapshot of an Excel report converted to HTML. 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. It gives users flexibility, as well as support for exporting workbooks, worksheets, or any specific range to HTML. GcExcel v3.2 helps businesses convert complex spreadsheets into easy-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.
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);
While converting the Excel reports to HTML, the HTML files are generated within the .zip directory. 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 and 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 known 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 the conversion, a zip file is created. The *.zip file includes:
- A *.HTML file for the exported workbook
- A folder that contains *.htm files for all WorkSheets
- A *.css file used for styling the HTML files
- *.png 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);
The above image shows what an exported workbook looks like in HTML. Note the multiple sheets of the workbook exported as separate tabs at the bottom of the exported HTML file.
How to Export a Worksheet to HTML
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 particular worksheet is helpful in scenarios where the user wants to present a specific worksheet on a dedicated web page.
Upon conversion, a zip file is created. The *.zip file includes:
- A .HTML file for the exported worksheet and a folder that contains a .htm file for the worksheet
- A *.css file used for styling the HTML file
- *.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);
How to Export a Range in a Workbook to HTML
Converting a specific cell range of a particular worksheet in a workbook is referred to as exporting a range. Export any cell range within any sheet of an Excel report to HTML with this option. This option is helpful in scenarios where an organization wants to present a specific range of a summary on its website.
Upon conversion, a zip file is created. The content of the *.zip file includes:
- A .HTML file for the exported range and a folder that contains a .htm file for the range
- A *.css file used for styling the HTML file
- *.png 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 a SpreadSheet to a Single HTML File
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).
While the HtmlSaveOptions class converts the Excel report to a single HTML file, it 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:
- There's only one sheet that needs to be exported or that the HtmlSaveOptions.ExportSheetName property is valid
- HtmlSaveOptions.ExportImageAsBase64 property is set to true
- HtmlSaveOptions.ExportCssSeperately property is set to false
- HtmlSaveOptions.ExportSingleTab property is set to false
//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);
How to Convert Excel Reports to HTML
Now, that we've learned about exporting Excel reports to HTML, let's build a Visual Studio application to export an Excel report to HTML.
Step 1. Installation
1.1 In your .Net Core Console application, right-click 'Dependencies' and select 'Manage NuGet Packages'
1.2 Under the 'Browse' tab, search for 'GrapeCity.Documents. Excel' and click Install
1.3 While installing, you'll receive two confirmation dialogs: 'Preview Changes' and 'License Acceptance,' click 'Ok' and 'I Agree' respectively
1.4 Also, add a reference to 'GrapeCity.Documents.DataVisualization.Chart', 'GrapeCity.Documents.Pdf', and 'System.IO.Packaging' packages
Step 2. C# project setup
2.1 Add namespace
In the Program file, import the following namespace:
using
GrapeCity.Documents.Excel;
2.2 Create a new workbook
In the main function, add the following code to create a new GcExcel workbook.
Workbook workbook = new Workbook ();
2.3 Load Excel workbook
In the main function, add the following code to load an Excel report as a file stream from the project's Resources.
var assembly =
typeof(HtmlExporting_Demo.Program)GetTypeInfo().Assembly; Stream
fileStream = assembly.GetManifestResourceStream("ProjectTrackerXlsx");
2.4 Open loaded FileStream in workbook
In the main function, invoke the workbook's Open.html) method to open the Excel report in GcExcel.
workbook.Open(fileStream);
2.5 Export loaded Excel workbook to HTML
In the main function, invoke the workbook's Save.html) method with the HTML option of SaveFileFormat enumeration.
workbook.Save(outputStream,SaveFileFormat.Html);
Invoking the Save method converts the Excel report to HTML.
Note:
While using an 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
- GcExcel for JAVA cannot export charts to HTML, it's currently a limitation
Download Demo application: HtmlExporting_Demo.zip