Render Excel and Apple Numbers spreadsheets as HTML, PDF, and image files
Leave feedback
On this page
GroupDocs.Viewer for .NET allows you to render your spreadsheet files in HTML, PDF, PNG, and JPEG formats. You do not need to use Microsoft Excel or other spreadsheet programs to load and view Excel documents within your .NET application (web or desktop).
To start with the GroupDocs.Viewer API, create a Viewer class instance. Pass a spreadsheet file you want to view to the class constructor. You can load the document from a file or stream. Call one of the Viewer.View method overloads to convert the document to HTML, PDF, or image format. These methods allow you to render the entire document or specific pages.
GroupDocs.Viewer can detect the document format automatically based on information in the file header.
Render spreadsheets as HTML
Create an HtmlViewOptions class instance and pass it to the Viewer.View method to convert a spreadsheet file to HTML. The HtmlViewOptions class properties allow you to control the conversion process. For instance, you can embed all external resources in the generated HTML file, minify the output file, and optimize it for printing. Refer to the following documentation section for details: Rendering to HTML.
When rendering spreadsheets to HTML format, regardless of the exact rendering mode (one HTML file per one worksheet, or single HTML for all worksheets, embedded or external resources), there always is an HTML <table>, that contains all the data from a worksheet, and a set of different CSS properties, which are applied to different parts of this table (rows, cells, or their ranges) and decorate them. There always is a CSS stylesheet (embedded or external) with a set of rulesets and at-rules, which are binded with a HTML table and its content by different selectors.
In some cases it is required to embed the GroupDocs.Viewer into the other web-application. In this scenario may occur an issue, when the styles from the GroupDocs.Viewer interfere with the styles of the parent web application. In particular, when GroupDocs.Viewer converts the spreadsheet to the HTML, it creates several rulesets with <TR>, <TD>, <COL>, <BR> type-selector, and these rulesets may corrupt the behavior of the parent web-page.
It was a problem in the GroupDocs.Viewer before the version 24.3. Starting from version 24.3, when rendering spreadsheet documents, the GroupDocs.Viewer adds the unique ID value to every HTML element and CSS selector in order to eliminate any interference with styles of the parent application. So now all the type-selectors are replaced to be the combined id-type descendant selectors like #gdt-id-1 td, #gdt-id-2 tr, and so on. This excludes any style conflicts once and for all.
Create an HTML file with embedded resources
To save all elements of an HTML page (including text, graphics, and stylesheets) into a single file, call the HtmlViewOptions.ForEmbeddedResources method and specify the output file name.
Convert an Excel workbook to HTML
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to HTML.// {0} is replaced with the current page number in the file names.varviewOptions=HtmlViewOptions.ForEmbeddedResources("page_{0}.html");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("invoice.xlsx")' Convert the spreadsheet to HTML.
' {0} is replaced with the current page number in the file names.
DimviewOptions=HtmlViewOptions.ForEmbeddedResources("page_{0}.html")viewer.View(viewOptions)EndUsingEndSubEndModule
The following image demonstrates the result:
Convert an Apple Numbers spreadsheet to HTML
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("Products.numbers")){// Convert the spreadsheet to HTML.// {0} is replaced with the current page number in the file names.varviewOptions=HtmlViewOptions.ForEmbeddedResources("page_{0}.html");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("Products.numbers")' Convert the spreadsheet to HTML.
' {0} is replaced with the current page number in the file names.
DimviewOptions=HtmlViewOptions.ForEmbeddedResources("page_{0}.html")viewer.View(viewOptions)EndUsingEndSubEndModule
The following image demonstrates the result:
Create an HTML file with external resources
If you want to store an HTML file and additional resource files (such as fonts, images, and stylesheets) separately, call the HtmlViewOptions.ForExternalResources method and pass the following parameters:
The output file path format
The path format for the folder with external resources
The resource URL format
Convert an Excel workbook to HTML
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to HTML.// Specify the HTML file names and location of external resources.// {0} and {1} are replaced with the current page number and resource name, respectively.varviewOptions=HtmlViewOptions.ForExternalResources("page_{0}.html","page_{0}/resource_{0}_{1}","page_{0}/resource_{0}_{1}");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("invoice.xlsx")' Convert the spreadsheet to HTML.
' Specify the HTML file names and location of external resources.
' {0} and {1} are replaced with the current page number and resource name, respectively.
DimviewOptions=HtmlViewOptions.ForExternalResources("page_{0}.html","page_{0}/resource_{0}_{1}","page_{0}/resource_{0}_{1}")viewer.View(viewOptions)EndUsingEndSubEndModule
Convert an Apple Numbers spreadsheet to HTML
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("Products.numbers")){// Convert the spreadsheet to HTML.// Specify the HTML file names and location of external resources.// {0} and {1} are replaced with the current page number and resource name, respectively.varviewOptions=HtmlViewOptions.ForExternalResources("page_{0}.html","page_{0}/resource_{0}_{1}","page_{0}/resource_{0}_{1}");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("Products.numbers")' Convert the spreadsheet to HTML.
' Specify the HTML file names and location of external resources.
' {0} and {1} are replaced with the current page number and resource name, respectively.
DimviewOptions=HtmlViewOptions.ForExternalResources("page_{0}.html","page_{0}/resource_{0}_{1}","page_{0}/resource_{0}_{1}")viewer.View(viewOptions)EndUsingEndSubEndModule
The image below demonstrates the result. External resources are placed in a separate folder.
Convert all Excel worksheets to one HTML file
To convert all worksheets to one HTML file, set the HtmlViewOptions.RenderToSinglePage property to true. This feature is supported in both cases - when converting to HTML with embedded and external resources.
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("Personal_net_worth_calculator.xlsx")){// Convert all Excel worksheets to one HTML file.varviewOptions=HtmlViewOptions.ForEmbeddedResources("page.html");// Enable converting all worksheets to one file.viewOptions.RenderToSinglePage=true;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("Personal_net_worth_calculator.xlsx")' Convert all Excel worksheets to one HTML file.
DimviewOptions=HtmlViewOptions.ForEmbeddedResources("page.html")' Enable converting all worksheets to one file.
viewOptions.RenderToSinglePage=Trueviewer.View(viewOptions)EndUsingEndSubEndModule
The following image demonstrates the result:
Render spreadsheets as PDF
Create a PdfViewOptions class instance and pass it to the Viewer.View method to convert a spreadsheet file to PDF. The PdfViewOptions class properties allow you to control the conversion process. For instance, you can protect the output PDF file, reorder its pages, and specify the quality of document images. Refer to the following documentation section for details: Rendering to PDF.
Convert an Excel workbook to PDF
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to PDF.varviewOptions=newPdfViewOptions("output.pdf");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("invoice.xlsx")' Convert the spreadsheet to PDF.
DimviewOptions=NewPdfViewOptions("output.pdf")viewer.View(viewOptions)EndUsingEndSubEndModule
The following image demonstrates the result:
Convert an Apple Numbers spreadsheet to PDF
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("Products.numbers")){// Convert the spreadsheet to PDF.varviewOptions=newPdfViewOptions("output.pdf");viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("Products.numbers")' Convert the spreadsheet to PDF.
DimviewOptions=NewPdfViewOptions("output.pdf")viewer.View(viewOptions)EndUsingEndSubEndModule
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to PNG.// {0} is replaced with the current page number in the file names.varviewOptions=newPngViewOptions("output_{0}.png");// Set width and height.viewOptions.Width=800;viewOptions.Height=900;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("invoice.xlsx")' Convert the spreadsheet to PNG.
' {0} is replaced with the current page number in the file names.
DimviewOptions=NewPngViewOptions("output_{0}.png")' Set width and height.
viewOptions.Width=800viewOptions.Height=900viewer.View(viewOptions)EndUsingEndSubEndModule
The following image demonstrates the result:
Convert an Apple Numbers spreadsheet to PNG
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("Products.numbers")){// Convert the spreadsheet to PNG.// {0} is replaced with the current page number in the file names.varviewOptions=newPngViewOptions("output_{0}.png");// Set width and height.viewOptions.Width=800;viewOptions.Height=900;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("Products.numbers")' Convert the spreadsheet to PNG.
' {0} is replaced with the current page number in the file names.
DimviewOptions=NewPngViewOptions("output_{0}.png")' Set width and height.
viewOptions.Width=800viewOptions.Height=900viewer.View(viewOptions)EndUsingEndSubEndModule
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to JPEG.// {0} is replaced with the current page number in the file names.varviewOptions=newJpgViewOptions("output_{0}.jpg");// Set width and height.viewOptions.Width=800;viewOptions.Height=900;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("invoice.xlsx")' Convert the spreadsheet to JPEG.
' {0} is replaced with the current page number in the file names.
DimviewOptions=NewJpgViewOptions("output_{0}.jpg")' Set width and height.
viewOptions.Width=800viewOptions.Height=900viewer.View(viewOptions)EndUsingEndSubEndModule
Convert an Apple Numbers spreadsheet to JPEG
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("Products.numbers")){// Convert the spreadsheet to JPEG.// {0} is replaced with the current page number in the file names.varviewOptions=newJpgViewOptions("output_{0}.jpg");// Set width and height.viewOptions.Width=800;viewOptions.Height=900;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("Products.numbers")' Convert the spreadsheet to JPEG.
' {0} is replaced with the current page number in the file names.
DimviewOptions=NewJpgViewOptions("output_{0}.jpg")' Set width and height.
viewOptions.Width=800viewOptions.Height=900viewer.View(viewOptions)EndUsingEndSubEndModule
Detect a CSV/TSV separator
If you load a CSV/TSV file to convert it to another format, enable the SpreadsheetOptions.DetectSeparator property for a target view to automatically detect a delimiter used to separate values in the source file.
GroupDocs.Viewer can detect the following separators:
A comma (the default separator)
A semicolon
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...using(varviewer=newViewer("sample.csv")){// Convert the spreadsheet to HTML.// {0} is replaced with the current page number in the file names.varviewOptions=HtmlViewOptions.ForEmbeddedResources("page_{0}.html");// Detect a CSV/TSV separator.viewOptions.SpreadsheetOptions.DetectSeparator=true;viewer.View(viewOptions);}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("sample.csv")' Convert the spreadsheet to HTML.
' {0} is replaced with the current page number in the file names.
DimviewOptions=HtmlViewOptions.ForEmbeddedResources("page_{0}.html")' Detect a CSV/TSV separator.
viewOptions.SpreadsheetOptions.DetectSeparator=Trueviewer.View(viewOptions)EndUsingEndSubEndModule
Get worksheet names
GroupDocs.Viewer allows you to obtain information about the source spreadsheet file. For example, you can retrieve worksheet names, as described below:
Call the Viewer.GetViewInfo method and pass the ViewInfoOptions instance to this method as a parameter.
Use the Pages property of the returned ViewInfo object to iterate through the list of worksheets and retrieve the worksheet names.
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;usingGroupDocs.Viewer.Results;// ...using(varviewer=newViewer("sample.xlsx")){varviewInfoOptions=ViewInfoOptions.ForHtmlView();// Call this method to create a single page for each worksheet.viewInfoOptions.SpreadsheetOptions=SpreadsheetOptions.ForOnePagePerSheet();varviewInfo=viewer.GetViewInfo(viewInfoOptions);// Print the worksheet names in the console window.Console.WriteLine("The document contains the following worksheets:");foreach(PagepageinviewInfo.Pages){Console.WriteLine($" - Worksheet {page.Number}: '{page.Name}'");}}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.OptionsImportsGroupDocs.Viewer.Results' ...
ModuleProgramSubMain(argsAsString())Usingviewer=NewViewer("sample.xlsx")DimviewInfoOptions=ViewInfoOptions.ForHtmlView()' Call this method to create a single page for each worksheet.
viewInfoOptions.SpreadsheetOptions=SpreadsheetOptions.ForOnePagePerSheet()DimviewInfo=viewer.GetViewInfo(viewInfoOptions)' Print the worksheet names in the console window.
Console.WriteLine("The document contains the following worksheets:")ForEachpageAsPageInviewInfo.PagesConsole.WriteLine($" - Worksheet {page.Number}: '{page.Name}'")NextEndUsingEndSubEndModule
The following image shows a sample console output: