Render Excel and Apple Numbers spreadsheets as HTML, PDF, and image files
Leave feedback
On this page
GroupDocs.Viewer for Java 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 Java 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 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 a HtmlViewOptions class instance and pass it to the 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.
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.
Example 1: Convert an Excel workbook to HTML
try(Viewerviewer=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.
HtmlViewOptionsviewOptions=HtmlViewOptions.forExternalResources("page_{0}.html","page_{0}/resource_{0}_{1}","page_{0}/resource_{0}_{1}");viewer.view(viewOptions);}
The following image demonstrates the result:
Example 2: Convert an Apple Numbers spreadsheet to HTML
try(Viewerviewer=newViewer("Products.numbers")){// Convert the spreadsheet to HTML.
// {0} is replaced with the current page number in the file names.
HtmlViewOptionsviewOptions=HtmlViewOptions.forEmbeddedResources("page_{0}.html");viewer.view(viewOptions);}
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
Example 1: Convert an Excel workbook to HTML
try(Viewerviewer=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.
HtmlViewOptionsviewOptions=HtmlViewOptions.forExternalResources("page_{0}.html","page_{0}/resource_{0}_{1}","page_{0}/resource_{0}_{1}");viewer.view(viewOptions);}
Example 2: Convert an Apple Numbers spreadsheet to HTML
try(Viewerviewer=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.
HtmlViewOptionsviewOptions=HtmlViewOptions.forExternalResources("page_{0}.html","page_{0}/resource_{0}_{1}","page_{0}/resource_{0}_{1}");viewer.view(viewOptions);}
The image below demonstrates the result. External resources are placed in a separate folder.
Render spreadsheets as PDF
Create a PdfViewOptions class instance and pass it to the 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.
Example 1: Convert an Excel workbook to PDF
try(Viewerviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to PDF.
PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");viewer.view(viewOptions);}
The following image demonstrates the result:
Example 2: Convert an Apple Numbers spreadsheet to PDF
try(Viewerviewer=newViewer("Products.numbers")){// Convert the spreadsheet to PDF.
PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");viewer.view(viewOptions);}
try(Viewerviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to PNG.
// {0} is replaced with the current page number in the file names.
PngViewOptionsviewOptions=newPngViewOptions("output_{0}.png");viewOptions.setWidth(800);viewOptions.setHeight(900);viewer.view(viewOptions);}
The following image demonstrates the result:
Example 2: Convert an Apple Numbers spreadsheet to PNG
try(Viewerviewer=newViewer("Products.numbers")){// Convert the spreadsheet to PNG.
// {0} is replaced with the current page number in the file names.
PngViewOptionsviewOptions=newPngViewOptions("output_{0}.png");viewOptions.setWidth(800);viewOptions.setHeight(900);viewer.view(viewOptions);}
try(Viewerviewer=newViewer("invoice.xlsx")){// Convert the spreadsheet to JPEG.
// {0} is replaced with the current page number in the file names.
JpgViewOptionsviewOptions=newJpgViewOptions("output_{0}.jpg");viewOptions.setWidth(800);viewOptions.setHeight(900);viewer.view(viewOptions);}
Example 2: Convert an Apple Numbers spreadsheet to JPEG
try(Viewerviewer=newViewer("Products.numbers")){// Convert the spreadsheet to JPEG.
// {0} is replaced with the current page number in the file names.
JpgViewOptionsviewOptions=newJpgViewOptions("output_{0}.jpg");viewOptions.setWidth(800);viewOptions.setHeight(900);viewer.view(viewOptions);}
Detect a CSV/TSV separator
If you load a CSV/TSV file to convert it to another format, enable the SpreadsheetOptions.setDetectSeparator(…) 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:
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 getPages() method of the returned ViewInfo object to iterate through the list of worksheets and retrieve the worksheet names.
try(Viewerviewer=newViewer("sample.xlsx")){ViewInfoOptionsviewInfoOptions=ViewInfoOptions.forHtmlView();// Call this method to create a single page for each worksheet.
viewInfoOptions.setSpreadsheetOptions(SpreadsheetOptions.forOnePagePerSheet());ViewInfoviewInfo=viewer.getViewInfo(viewInfoOptions);// Print the worksheet names in the console window.
System.out.println("The document contains the following worksheets:");for(Pagepage:viewInfo.getPages()){System.out.println(" - Worksheet {page.Number}: '"+page.getName()+"'");}}
The following image demonstrates a sample console output:
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.