Converting Workbook to Different Formats

Converting Excel to XPS

The XPS document format consists of structured XML markup that defines the layout of a document and the visual appearance of each page, along with rendering rules for distributing, archiving, rendering, processing and printing documents.

The markup language for XPS is a subset of XAML which allows it to incorporate vector graphic elements in documents, using XAML to mark up the Windows Presentation Foundation (WPF) primitives. The elements used are described in terms of paths and other geometrical primitives.

An XPS file is in fact a Unicoded ZIP archive using the Open Packaging Conventions, containing the files which make up the document. These include an XML markup file for each page, text, embedded fonts, raster images, 2D vector graphics, as well as the digital rights management information. The contents of an XPS file can be examined simply by opening it in an application that supports ZIP files.

From Aspose.Cells 6.0.0, Microsoft Excel tp XPS conversion is supported.

Converting single Worksheet to XPS

The following example shows how to convert a single worksheet in an Excel file to XPS.

Export Whole Workbook to XPS

The following example shows how to convert the whole workbook to XPS format.

Quick Excel to XPS Conversion

The following example shows a simple way to directly convert the Excel file to XPS format.

Converting Excel to MHTML Files

MHTML combines normal HTML with external resources; that is, content that is usually linked in like images, animations, audio and so on into one file. They are used for emails with the .mht file extension.

Converting a spreadsheet to MHTML is a quick operation, as shown below.

The code example below shows how to save a workbook as an MHTML file.

Converting Excel Files to HTML

The Aspose.Cells APIs provides support for exporting spreadsheets to HTML format. For this purpose, Aspose.Cells uses the HtmlSaveOptions class which allows developers to control several aspects of the output HTML.

The code below demonstrates how to use the HtmlSaveOptions class to export Microsoft Excel files to HTML format without specifying additional parameters.

Setting Image Preferences for HTML

Starting from 8.0.2, Aspose.Cells has exposed ImageOptions for the HtmlSaveOptions class, which allows developers to specify image preferences when saving spreadsheets to HTML format.

The image settings that can be applied are:

  • ImageType: Gets or sets the image type. Please note, all shapes, including charts, render as images in the output HTML.
  • Quality: Gets or sets the quality of images between 0 to 100, when ImageFormat is specified as Jpeg.
  • VerticalResolution: Gets or sets the vertical resolution of the image in dots per inch.
  • HorizontalResolution: Gets or sets the horizontal resolution of the image in dots per inch.
  • TiffCompression: Gets or sets the compression type for the images when ImageFormat is specified as Tiff.
  • Transparent: Indicates if the background of an image should be transparent when ImageFormat is specified as Png.

The code below demonstrates how to use HtmlSaveOptions.ImageOptions to specify different preferences.

Spreadsheet view before export HTML view after export
Spreadsheet view before export HTML view after export

Converting Excel to PDF Files

PDF documents are widely used as a standard format of exchanging documents between organizations, government sectors, and individuals. Software developers are often asked to device a way to easily convert Microsoft Excel files into PDF documents. Aspose.Cells supports these features. This article shows how.

Converting Excel to PDF

Microsoft Excel to PDF conversion was introduced with Aspose.Cells for Java 2.3.0. From that release, Aspose.Cells can convert spreadsheets to PDF directly (including PDF/A), without another product. To convert spreadsheets with older versions of Aspose.Cells, use Aspose.PDF for the conversion.

Aspose.Cell’s converts spreadsheets to PDF with a high degree of accuracy and fidelity. However, there are a few limitations, listed at the end of this article.

Direct Conversion

Save an Excel file directly to PDF using the Workbook.save method, and provide the SaveFormat.PDF interface member. Direct conversion like this is the most efficient conversion method. It does not lose data or formatting but keeps the output PDF looking like the input Excel file.

To specify security options when saving to PDF, use PdfSaveOptions.

Advanced Conversion

You may also opt to use the PdfSaveOptions class to set different attributes for the conversion. Setting different properties of PdfSaveOptions class will give you the control over the Print, Font, Security and Compression settings for the resultant PDF file. Most notable property is the Compliance that enables you to save the Excel files to PDF/A compliant PDF files.

Saving Excel Spreadsheets to PDF/A Complied Files

Below provided code snippet demonstrates the usage of the PdfSaveOptions class to save the Excel files to PDF/A compliant PDF format.

Conversion with Aspose.Pdf: Aspose.Cells Prior to 2.3.0

For Aspose.Cells versions prior to version 2.3.0 you need to use a component like Aspose.PDF for Java to convert spreadsheets to PDF files. Aspose.Cells and Aspose.PDF work together to convert a spreadsheet to PDF via an intermediate step.

To convert spreadsheets to PDF with Aspose.Cells and Aspose.PDF:

  1. Instantiate an object of the Workbook class by calling its empty constructor.
  2. Do your desired work on the spreadsheet using the Aspose.Cells API.
  3. Call the Workbook.save method to save the spreadsheet:
    1. Set the file format to XML.
    2. Select Aspose_Pdf (a pre-defined value) from the FileFormatType interface. This directs the save method to generate a spreadsheet in the XML form compatible with the Aspose.PDF Schema so that Aspose.PDF for Java can then generate a PDF document.
  4. When the XML file has been created, create an object of the Pdf class in the aspose.pdf package.
  5. Call the Pdf class' bindXML method and pass the name of the output XML file.
  6. Call the Pdf class' save method to generate the PDF document.

The above steps are implemented below in an example.

Conversion Attributes

We work hard to improve conversion and other aspects of Aspose.Cells with every release. Excel to PDF conversion has a few limitations. Some format settings specified in a spreadsheet might be lost, and not all drawing objects are supported.

The table below lists all features that are fully or partially supported when exporting to PDF using Aspose.Cells. This table is not final and does not cover all the spreadsheet attributes. It can also identify those features that may not be supported or are partially supported for the conversion.