Render Excel and Apple Numbers spreadsheets as HTML, PDF, and image files

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.

View Excel files online View demos and examples on GitHub

Supported spreadsheet file formats

GroupDocs.Viewer supports the following spreadsheet file formats:

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.

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

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("invoice.xlsx"))
{
    // Convert the spreadsheet to HTML.
    // {0} is replaced with the current page number in the file names.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html");
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render an Excel file to HTML

Convert an Apple Numbers spreadsheet to HTML

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("Products.numbers"))
{
    // Convert the spreadsheet to HTML.
    // {0} is replaced with the current page number in the file names.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html");
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render an Apple Numbers spreadsheet to HTML

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

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("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.
    var viewOptions = HtmlViewOptions.ForExternalResources("page_{0}.html", "page_{0}/resource_{0}_{1}", "page_{0}/resource_{0}_{1}");
    viewer.View(viewOptions);
}

Convert an Apple Numbers spreadsheet to HTML

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("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.
    var viewOptions = 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.

Place HTML resources 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.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("Personal_net_worth_calculator.xlsx"))
{
    // Convert all Excel worksheets to one HTML file.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("page.html");
    // Enable converting all worksheets to one file.
    viewOptions.RenderToSinglePage = true;
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Convert all Excel worksheets to one HTML file

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

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("invoice.xlsx"))
{
    // Convert the spreadsheet to PDF.
    var viewOptions = new PdfViewOptions("output.pdf");
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render an Excel file to PDF

Convert an Apple Numbers spreadsheet to PDF

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("Products.numbers"))
{
    // Convert the spreadsheet to PDF.
    var viewOptions = new PdfViewOptions("output.pdf");
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render an Apple Numbers spreadsheet to PDF

Optimize (compress) output PDF

Optimized PDF document won’t contain common fonts like Times New Roman and Arial. Grid lines is also optimized to reduce output file size. Depending on the content of the input spreadsheet the output PDF size can be reduced up to 40%. To optimize the output PDF set PdfViewOptions.Optimize property to true.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...
using (var viewer = new Viewer("employees.xlsx"))
{
    var viewOptions = new PdfViewOptions();
    viewOptions.Optimize = true;
    viewer.View(viewOptions);
}

The following image shows the properties of two PDF files where the left one is not optimized and has a size of 11.2 MB and the right one is 6.6 MB.

Not optimized and optimized PDF files comparison

Render spreadsheets as PNG

Create a PngViewOptions class instance and pass it to the Viewer.View method to convert a spreadsheet file to PNG. Use the PngViewOptions.Height and PngViewOptions.Width properties to specify the output image size in pixels.

Convert an Excel workbook to PNG

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("invoice.xlsx"))
{
    // Convert the spreadsheet to PNG.
    // {0} is replaced with the current page number in the file names.
    var viewOptions = new PngViewOptions("output_{0}.png");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 900;
    viewer.View(viewOptions);
}
The following image demonstrates the result:

Render an Excel file to PNG

Convert an Apple Numbers spreadsheet to PNG

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("Products.numbers"))
{
    // Convert the spreadsheet to PNG.
    // {0} is replaced with the current page number in the file names.
    var viewOptions = new PngViewOptions("output_{0}.png");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 900;
    viewer.View(viewOptions);
}

The following image demonstrates the result:

Render an Apple Numbers spreadsheet to PNG

Render spreadsheets as JPEG

Create a JpgViewOptions class instance and pass it to the Viewer.View method to convert a spreadsheet file to JPEG. Use the JpgViewOptions.Height and JpgViewOptions.Width properties to specify the output image size in pixels.

Convert an Excel workbook to JPEG

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("invoice.xlsx"))
{
    // Convert the spreadsheet to JPEG.
    // {0} is replaced with the current page number in the file names.
    var viewOptions = new JpgViewOptions("output_{0}.jpg");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 900;
    viewer.View(viewOptions);
}

Convert an Apple Numbers spreadsheet to JPEG

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer("Products.numbers"))
{
    // Convert the spreadsheet to JPEG.
    // {0} is replaced with the current page number in the file names.
    var viewOptions = new JpgViewOptions("output_{0}.jpg");
    // Set width and height.
    viewOptions.Width = 800;
    viewOptions.Height = 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.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
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

using (var viewer = new Viewer(@"sample.csv"))
{
    // Convert the spreadsheet to HTML.
    // {0} is replaced with the current page number in the file names.
    var viewOptions = HtmlViewOptions.ForEmbeddedResources("page_{0}.html");
    // Detect a CSV/TSV separator.
    viewOptions.SpreadsheetOptions.DetectSeparator = true;
    viewer.View(viewOptions);
}          

Detect a CSV/TSV encoding

To detect CSV/TSV file encoding, enable the LoadOptions.DetectEncoding property. When Viewer fails to detect encoding it falls back to LoadOptions.Encoding property value.

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...
var loadOptions = new LoadOptions();
loadOptions.DetectEncoding = true;
using (var viewer = new Viewer("employees.csv", loadOptions))
{
    var viewOptions = HtmlViewOptions.ForEmbeddedResources();
    viewer.View(viewOptions);
}          

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:

  1. Create a ViewInfoOptions instance for a specific view.
  2. Call the Viewer.GetViewInfo method and pass the ViewInfoOptions instance to this method as a parameter.
  3. Use the Pages property of the returned ViewInfo object to iterate through the list of worksheets and retrieve the worksheet names.
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer.Results;
// ...

using (var viewer = new Viewer("sample.xlsx"))
{
    var viewInfoOptions = ViewInfoOptions.ForHtmlView();
    // Call this method to create a single page for each worksheet.
    viewInfoOptions.SpreadsheetOptions = SpreadsheetOptions.ForOnePagePerSheet();

    var viewInfo = viewer.GetViewInfo(viewInfoOptions);

    // Print the worksheet names in the console window.
    Console.WriteLine("The document contains the following worksheets:");
    foreach (Page page in viewInfo.Pages)
    {
        Console.WriteLine($" - Worksheet {page.Number}: '{page.Name}'");
    }
}

The following image shows a sample console output:

Retrieve worksheet names

See also