Purpose
This document is designed to provide the developers with comprehensive understanding on how an excel document (containing images, charts, formatting etc.) can be transformed to PDF file.
Introduction
Since we all know the great essence of Excel files remains in: the cells are organized in rows and columns into different worksheets, and contain all types of data or formulas with relative or absolute references to other cells. The Intuitive interface related formatting and capable calculation and graphing ability which have made it the dominant spreadsheet in the market today. It not only has editing options, but also possesses the versatility of spreadsheet management. This makes it easier to perform statistical analyses and manipulations on any type of data of any volume.
Similarly, PDF (Portable Document Format) has its own advantages. The file format represents documents in a manner that is independent of the original application software, hardware, and operating system used to create those documents. A PDF file can describe documents containing any combination of text, graphics, and images in a device independent and resolution independent manner. PDF files are often compressed, taking up less space than the original file.
At times, you do need to convert an Excel File to PDF Format. This might be the demand for modern business, governmental, academic or non-profit environment. You actually want an affordable and easier way to create professional-quality documents in the popular PDF file format which should be correctly rendered or converted. You require fast, secure, accurate and reliable tool / utility, component or solution to distribute a PDF document and exchange the electronic document around the world,
You might come across numerous conversion tools / utilities and components for the task. But you have to make sure that while converting an XLS file to PDF format, the layout of the original Excel document is retained in the output PDF file. The images, data formatting, fonts, attributes, colors, page setup settings, text orientation, borders, charts etc. should be rendered accurately and precisely without loosing data, formatting or objects in the output file. In this article I choose two Aspose components which can provide your desired results: Aspose.Cells and Aspose.Pdf.
Aspose.Cells is an Excel® spreadsheet reporting component that enables you to read and write Excel® spreadsheets without utilizing Microsoft Excel® installed either on the client or server side. Aspose.Cells is a very feature rich component that offers much more than just basic exporting of data. With Aspose.Cells, developers can export data, format spreadsheets in every detail and at every level, import images, import charts, create charts, manipulate charts, stream Excel® data, save in various formats including XLS, CSV, SpreadsheetML, TabDelimited, TXT, XML (Aspose.Pdf integrated) and many more. Aspose.Cells has a huge list of features. To know more about the product information, features and for a programmer's guide, please check its summary of Features List , Aspose.Cells Decumentation and online featured demos. You may download its evaluation version for free.
Aspose.Pdf is a non-graphical PDF® document reporting component that enables either .Net or Java applications to create PDF documents without utilizing Adobe Acrobat®. Aspose.Pdf is very affordably priced and offers a wealth of strong features including: compression, tables, graphs, images, hyperlinks, security and custom fonts. Aspose.Pdf supports creation of PDF files through both an API and from XML templates and XSL-FO files. To know more about the product information, features and for a programmer's guide, please check its summary of Features List , Aspose.Pdf Decumentation and online featured demos. You may download its evaluation version for free.
Aspose : The Brand
Aspose provides a great line of .NET and Java components. Trusted by thousands of customers worldwide, the products include File Format Components, Reporting Products, Visual Components and Utility Components. Aspose File Format Components include products such as Aspose.Words, Aspose.Cells, Aspose.Pdf, Aspose.Slides and so on that allow to programmatically open, modify, generate, save, merge, convert etc documents in various formats including DOC, RTF, WordML, HTML, PDF, XLS, PPT and others. These products are .NET class libraries that developers use when building their .NET or Java applications that require access to documents in different formats. Aspose File Format Components are often chosen for their superior performance, scalability and stability in a server environment over Microsoft Office Automation. Aspose also addresses the powerful integration options b/w its different components to enhance a product's productivity and effectiveness for a common user. You can easily convert from one document format to another.
In this article, we create a simple console application in Visual Studio.Net that will convert an Excel File to PDF format using Aspose.Cells API and Aspose.Pdf API. The conversion is performed with high degree of precision and accuracy.
Performing the Task
For demonstration, I utilize an Excel file (PurchasingReport.xls) as a template. The workbook has some worksheets and exhibits some purchasing design models. Each worksheet is furnished with different types of format settings using fonts, attributes, colors, shading effects and borders. A picture is inserted in the first worksheet and a column chart is created in the last worksheet for further examination. I covert the template to PDF file using the simplest APIs of Aspose.Cells and Aspose.Pdf with a few lines of code. For conversion we shall create a console project.
Converting the XLS file to PDF Format
First we create the excel file in MS Excel and next we will convert it programmatically to PDF file format.
The Template Excel File
The template file demonstrates some design models related purchasing. It has quite a number of worksheets i.e., Summary, Award1, Award2, Invent1, Invent2, Media, Purchase and Media Chart. The first worksheet has an image plus some page setup options related header and footer. The Media chart worksheet shows a column chart based on Media costs. Following are some screenshots of the template file:
i) This is the screenshot of the first worksheet named "Summary"; you may see a picture here.

ii) This is the screenshot of the third worksheet named "Award2".
iii) This is the screenshot of the fifth worksheet named "Invent2".
iv) This is the screenshot of the last worksheet named "Media Chart". You may find a column chart here based on some media costs.
Converting XLS2PDF Format
Now I convert the file to PDF file.
Download and Install Aspose.Cells
First, you need to download Aspose.Cells for .Net and Aspose.Pdf for .Net components. Install them on your development computer. All Aspose components, when installed, work in evaluation mode. The evaluation mode has no time limit and it only injects watermarks into produced documents.
Create a Project
Start Visual Studio. Net and create a new console application. This example will show a C# console application, but you can use VB.NET too.
Add References
This project will use Aspose.Cells and Aspose.Pdf components. So, you have to add references to both components in your project:
- add a reference to ….\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll
- add a reference to ….\Program Files\Aspose\Aspose.Pdf\Bin\Net1.0\Aspose.Pdf.dll
Code Snippet
Following is the actual code (Written in C#) used by the components to accomplish the task. You may see that very few lines of code are used to accomplish the task.
//--------------------------------------------------------------------
//--- AsposeExcel2PdfConverter
//--- A Console Application which converts an excel file to pdf format.
//---
//--- The Application utilizes a template excel file from Files folder
//--- in the Application directory and converts it to pdf file.
//---
//--- © Aspose 2007. All rights reserved.
//--- Support: http://www.aspose.com/Community/forums/
//---
//--------------------------------------------------------------------
using System;
using System.IO;
using System.Windows.Forms;
using Aspose.Cells;
using Aspose.Pdf;
namespace AsposeExcel2PdfConverter
{
/// <summary>
/// AsposeExcel2PdfConverter
/// Use Aspose.Cells and Aspose.Pdf to perform the task
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
// Uncomment the code below when you have purchased
// license for Aspose.Cells and Aspose.Pdf. You need to
// deploy the licenses in the same folder as your
// executable, alternatively you can add the license
// files as an embedded resource to your project.
// // Set license for Aspose.Cells
// Aspose.Cells.License cellsLicense = new
// Aspose.Cells.License();
// cellsLicense.SetLicense("Aspose.Cells.lic");
//
// // Set license for Aspose.Pdf
// Aspose.Pdf.License pdfLicense = new
// Aspose.Pdf.License();
// pdfLicense.SetLicense("Aspose.Pdf.lic");
//Get the path of the Application folder.
string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "..\\..");
//Get the template excel file path.
string designerFile = path + @"\Files\PurchasingReport.xls";
//Specify the xml file path.
string xmlFile = path + @"\Files\PurchasingReport.xml";
//Specify the pdf file path.
string pdfFile = path + @"\Files\PurchasingReport.pdf";
//Create a new Workbook.
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
//Open the template excel file which you have to
//convert to pdf file.
wb.Open(designerFile);
//First, Save the xls file in xml file (Aspose.Pdf
//integrated)
wb.Save(xmlFile, FileFormatType.AsposePdf);
//Create a new Pdf object.
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
//Now, Bind the xml file (Aspose.Pdf integrated), which
//is used as a medium file for conversion.
pdf.BindXML(xmlFile, null);
//Save the pdf file.
pdf.Save(pdfFile);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}
Additionally I write its parallel VB.Net code here for your convenience:
'--------------------------------------------------------------------
'--- AsposeExcel2PdfConverter
'--- A Console Application which converts an excel file to pdf format.
'---
'--- The Application utilizes a template excel file from Files folder
'--- in the Application directory and converts it to pdf file.
'---
'--- © Aspose 2007. All rights reserved.
'--- Support: http://www.aspose.com/Community/forums/
'---
'--------------------------------------------------------------------
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports Aspose.Cells
Imports Aspose.Pdf
'AsposeExcel2PdfConverter
'Use Aspose.Cells and Aspose.Pdf to perform the task
Module Module1
Sub Main()
Try
' Uncomment the code below when you have purchased license
' for Aspose.Cells and Aspose.Pdf. You need to deploy the
' licenses in the same folder as your executable, alternatively
' you can add the license files as an embedded resource to your
' project.
' ' Set license for Aspose.Cells
' Dim cellsLicense As Aspose.Cells.License = New
' Aspose.Cells.License()
' cellsLicense.SetLicense("Aspose.Cells.lic")
'
' ' Set license for Aspose.Pdf
' Dim pdfLicense As Aspose.Pdf.License = New
' Aspose.Pdf.License()
' pdfLicense.SetLicense("Aspose.Pdf.lic")
'Get the path of the Application folder.
Dim path As String = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "..\")
'Get the template excel file path.
Dim designerFile As String = path + "\Files\PurchasingReport.xls"
'Specify the xml file path.
Dim xmlFile As String = path + "\Files\PurchasingReport.xml"
'Specify the pdf file path.
Dim pdfFile As String = path + "\Files\PurchasingReport.pdf"
'Create a new Workbook.
Dim wb As Aspose.Cells.Workbook = New Aspose.Cells.Workbook()
'Open the template excel file which you have to convert
'to pdf file.
wb.Open(designerFile)
'First, Save the xls file in xml file (Aspose.Pdf integrated)
wb.Save(xmlFile, FileFormatType.AsposePdf)
'Create a new Pdf object.
Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()
'Now, Bind the xml file (Aspose.Pdf integrated), which is
'used as a medium file for conversion.
pdf.BindXML(xmlFile, Nothing)
'Save the pdf file.
pdf.Save(pdfFile)
Catch e As Exception
Console.WriteLine(e.Message)
Console.ReadLine()
End Try
End Sub