We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert an Excel Chart To Image

Purpose

This document is designed to provide the developers with a detailed understanding on how an excel chart can be exported as an image.

Introduction

Charts are visually appealing and make it easy for users to see comparisons, patterns, and trends in data. For instance, rather than having to analyze several columns of worksheet numbers, you can see at a glance whether sales are falling or rising over quarterly periods, or how the actual sales compare to the projected sales.

Sometimes, you do need to present the chart in your applications or web pages. You might need to insert it into a Word Document, a PDF file, a Power Point Presentation or in some other scenario. Simply you want the chart should be rendered as an image, so that you may paste it into your applications with ease. A Picture is worthwhile. Frequently, in the course of work, one has to present statistical and graphical information in an easy to understand and an easy to maintain manner.

You might try Office Automation but Office automation has its own drawbacks. There are several reasons and issues involved: E.g., Security, Stability, Scalability/Speed, Price, Features etc. In Short, there are many reasons, with the top one being that Microsoft themselves strongly recommends against Office automation from software solutions

There is another option which you can use to convert an Excel Chart to an image using Aspose.Cells. This is a brand new feature offered by Aspose.Cells which is more advanced and of the essence from many of its Features List. In this article, we create some console applications in Visual Studio.Net, that will convert the Excel Charts to render the image files with a few and simplest lines of code using Aspose.Cells API. The conversion is performed with precision and accuracy.

Aspose.Cells : The Real Product

I would take this opportunity to introduce the product to you. 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. All the Aspose components are totally independent and are not affiliated with, nor authorized, sponsored by Microsoft Corporation. To know more about the product information, features and for a programmer’s guide, check Aspose.Cells Decumentation and online featured demos. You may download its evaluation version for free.

Performing the Task

For demonstration, I choose some common tasks here. I have created two charts (a pie chart and a column chart) in MS Excel and covert these charts to image files using the simplest API of Aspose.Cells with a few lines of code. For conversion we shall create two console applications.

Following is the task list:

· Converting a Pie Chart to an Image File.

· Converting a Column Chart to an Image File.

Converting a Pie Chart to an Image File

First we create a pie chart in MS Excel and next we convert the chart to an image file.

Creating a Pie Chart in MS Excel

I opened a new workbook in MS Excel and input some sales data of the different regions into a worksheet. I, then, created a pie chart based on the data and save the excel file. Following is the screenshot of the file created in MS Excel:

Converting Chart2Image

Now I convert the chart to an image file.

Download and Install Aspose.Cells

First, you need to download Aspose.Cells for .Net. Install it 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. So, you have to add reference to Aspose.Cells component in your project. E.g., add a reference to ….\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll

Code Snippet

Following is the actual code (Written in C#) used by the component to accomplish the task. You may see that very few lines of code are used to accomplish the task.

//Create a new workbook.

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();

//Open the existing excel file which contains the pie chart.

workbook.Open("d:\\piechart.xls");

//Get the designer chart (first chart) in the first worksheet.

//of the workbook.

Chart chart = workbook.Worksheets[0].Charts[0];

//Convert the chart to an image file.

System.Drawing.Bitmap bitmap = chart.ToImage();

//Save the chart image (jpg) file to disk.

bitmap.Save(@"d:\PieChart.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

Additionally I write its parallel VB.NET code here for your convenience:

'Create a new workbook.

Dim workbook As Aspose.Cells.Workbook = New Aspose.Cells.Workbook()

'Open the existing excel file which contains the pie chart.

workbook.Open("d:\piechart.xls");

'Get the designer chart (first chart) in the first worksheet.

'of the workbook.

Dim chart As Chart = workbook.Worksheets(0).Charts(0)

'Convert the chart to an image file.

Dim bitmap As System.Drawing.Bitmap = chart.ToImage()

'Save the chart image (jpg) file to disk.

bitmap.Save("d:\PieChart.jpg",System.Drawing.Imaging.ImageFormat.Jpeg)

After executing the above code, a jpg file is created based on the pie chart in the template excel file. Here is the screenshot of the output file:

Converting a Column Chart to an Image File

First we create a column chart in MS Excel and next we convert the chart to an image file as we did it before.

Creating a Column Chart in MS Excel

I opened a new workbook in MS Excel and input some marketing costs of the different regions into a worksheet. I, then, created a column chart based on the data and save the excel file. Following is the screenshot of the file created in MS Excel:

Converting Chart2Image

Now I convert the chart to an image file. I‘ll skip the conventional steps here like creating a new project in Visual Studio.NET or adding reference to Aspose.Cells.dll file. I already did it in the first task. I only write here the actual code with presentation of the output image file.

Code Snippet

Following is the actual code (Written in C#) used by the component to accomplish the task. The code is similar to the previous one:

//Create a new workbook.

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();

//Open the existing excel file which contains the column chart.

workbook.Open("d:\\columnchart.xls");

//Get the designer chart (first chart) in the first worksheet.

//of the workbook.

Chart chart = workbook.Worksheets[0].Charts[0];

//Convert the chart to an image file.

System.Drawing.Bitmap bitmap = chart.ToImage();

//Save the chart image (jpg) file to disk.

bitmap.Save(@"d:\ColumnChart.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

Additionally I write its parallel VB.NET code here for your convenience:

'Create a new workbook.

Dim workbook As Aspose.Cells.Workbook = New Aspose.Cells.Workbook()

'Open the existing excel file which contains the column chart.

workbook.Open("d:\columnchart.xls");

'Get the designer chart (first chart) in the first worksheet.

'of the workbook.

Dim chart As Chart = workbook.Worksheets(0).Charts(0)

'Convert the chart to an image file.

Dim bitmap As System.Drawing.Bitmap = chart.ToImage()

'Save the chart image (jpg) file to disk.

bitmap.Save("d:\ColumnChart.jpg",System.Drawing.Imaging.ImageFormat.Jpeg)

After executing the above code, a jpg file is created based on the column chart in the template excel file. Here is the screenshot of the output file:

Summary

In this article I have presented how can we convert an excel chart to render an image file using Aspose.Cells component. Hopefully, it will give you some insight, and you will be able to utilize it some scenarios. Since the Chart2Image feature was really demanding and required by some of Aspose customers. And this is the result of some workout by Aspose.Cells Product Team. The feature is still young, so there might be some precision and accuracy issues occurred in some cases. We are working to enhance the conversion to make it more elegant and sophisticated and will mend any deficiency if found to strengthen it more.

Aspose.Cells can offer more flexibility than others for solutions and provides outstanding speed, efficiency and reliability to meet specific business application requirements. The results do show that Aspose.Cells has benefited from years of research, design and careful tuning.

We heartily welcome your queries, comments and suggestions at Aspose.Cells Forum. We warranty a prompt reply within minutes or hours, Thank you!

Aspose - The .NET & Java component publisher

I downloaded the evaluation version and tried the code provided ablove.



It was all good before the line

System.Drawing.Bitmap bitmap = chart.ToImage();



It found the chart in my excel worksheet. But when I step over this line of code, I got an error:

An unhandled exception of type ‘System.ArgumentNullException’ occurred in mscorlib.dll



Additional information: Value cannot be null.



What did I do wrong?



Thanks a lot.

The 2.0 version ran with no error and the image was produced. But the numbers beside and below the chart were all wrong.

Your problem may be caused by that the chart type in your Excel file is not supported. Now Aspose.Cells only supports: Line chart, Column chart, Bar chart and Pie chart.



You can post your file and problem in our support forums: www.aspose.com/forums. Our developers will support you very soon.

@Jasony001,

Aspose.Cells has enhanced Chart to image, Chart to PDF and Excel to PDF features. We have made the features more robust and efficient with lots of enhancements, Aspose.Cells’ Chart to image and Chart to PDF features produce results with high fidelity and accuracy.

For more information on converting Excel files to PDF, Sheet to image, rendering chart to image and chart to PDF features, please follow the document links below:
Convert Excel Workbook to PDF
Chart Rendering
Converting Worksheet to Image using ImageOrPrint Options

Please download the latest version of Aspose.Cells for .NET from the following link:
Aspose.Cells for .NET (Latest Version)

You can also download the latest demos here for your complete reference.