How to Export and Save Excel Chart as Image?
The Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, open, and edit Excel documents without the dependency of interop Excel library. Also, converts Excel documents to image, PDF, HTML, and much more.
This article demonstrates how to export and save Excel chart as image in C# and VB.NET by using Syncfusion Excel (XlsIO) library.
Steps to export and save Excel chart as image, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install Syncfusion.ExcelChartToImageConverter.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.
Install ExcelChartToImageConverter NuGet package
Step 3: Include the following namespaces in the Program.cs file.
C#
using Syncfusion.XlsIO; using Syncfusion.ExcelChartToImageConverter; using System.IO; using System.Drawing;
VB.NET
Imports Syncfusion.XlsIO Imports Syncfusion.ExcelChartToImageConverter Imports System.IO Imports System.Drawing
Step 4: Add the below code snippet for Excel imaging or saving the file from Excel to PNG. The description of some important class members used in the code example are as follows:
- ExcelEngine should be initialized to access the Excel object.
- ChartToImageConverter should be initialized for Excel imaging.
- The ScalingMode property can be used to set the quality of the image.
- The Open() method of IWorkbooks interface opens the existing Excel workbook from the specified location. Moreover, you can also open the specified stream.
- You can access any of the Worksheets with its zero-based index for further manipulations.
- Similarly, you can access any of the Charts with its zero-based index.
- Most importantly, the SaveAsImage() method can be used to save Excel chart as image.
The following code snippet in C# and VB.NET exports and saves the Excel chart as image file in PNG format.
//Instantiate the spreadsheet creation engine using (ExcelEngine excelEngine = new ExcelEngine()) { //Initialize application IApplication application = excelEngine.Excel; //Set default application version application.DefaultVersion = ExcelVersion.Xlsx; //Instantiate chart to image converter for Excel imaging application.ChartToImageConverter = new ChartToImageConverter(); //Set the image quality application.ChartToImageConverter.ScalingMode = ScalingMode.Normal; //Open the existing Excel workbook containing chart string inputFileName = "Sample.xlsx"; IWorkbook workbook = application.Workbooks.Open(inputFileName, ExcelOpenType.Automatic); //Access the first worksheet from the worksheets collection IWorksheet worksheet = workbook.Worksheets[0]; //Access the first chart from the charts collection IChart chart = worksheet.Charts[0]; //Create the memory stream for chart image MemoryStream chartStream = new MemoryStream(); //Save Excel chart as image chart.SaveAsImage(chartStream); chartStream.Position = 0; //Get the image file from stream Image image = Image.FromStream(chartStream); //Save the image in png format image.Save("Output.png"); }
'Instantiate the spreadsheet creation engine Using excelEngine As ExcelEngine = New ExcelEngine 'Initialize application Dim application As IApplication = excelEngine.Excel 'Set default application version as Xlsx application.DefaultVersion = ExcelVersion.Xlsx 'Instantiate chart to image converter for Excel imaging application.ChartToImageConverter = New ChartToImageConverter() 'Set the image quality application.ChartToImageConverter.ScalingMode = ScalingMode.Normal 'Open the existing Excel workbook containing chart Dim inputFileName As String = "Sample.xlsx" Dim workbook As IWorkbook = application.Workbooks.Open(inputFileName, ExcelOpenType.Automatic) 'Access the first worksheet from the worksheets collection Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Access the first chart from the charts collection Dim chart As IChart = worksheet.Charts(0) 'Create the memory stream for chart image Dim chartStream As MemoryStream = New MemoryStream() 'Save Excel chart as image chart.SaveAsImage(chartStream) chartStream.Position = 0 'Get the image file from stream Dim image As Image = image.FromStream(chartStream) 'Save the image in png format image.Save("Output.png") End Using
A complete working example of Excel to PNG along with the input Excel file used can be downloaded from Export and Save Excel Chart as Image.zip.
By executing the program, you will get the Excel imaging output as shown below.
Excel to PNG image output
Take a moment to explore the rich set of Syncfusion Excel (XlsIO) library features.
Besides, here is an online sample link to open Excel file.
To learn more about the Syncfusion Excel (XlsIO) library, refer to the documentation where you will find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions with code examples.
See Also:
Utility to compare chart images converted using Microsoft Excel and Syncfusion XlsIO
How to set image quality during chart to image or PDF conversion in C#, VB.NET?
Is Chart to image conversion is supported in .net framework 3.5?
Create an Excel file in C# and VB.NET
How to convert Excel to PDF in C#, VB.NET
Convert Excel file to CSV in C# and VB.NET
Create Excel from DataTable in C# and VB.NET
Create an Excel file in ASP.NET Core
Create an Excel file in ASP.NET MVC
Create an Excel file in ASP.NET Web Forms
Create an Excel file in Xamarin
Create an Excel file in Xamarin.Android
Create an Excel file in Xamarin.iOS
Create an Excel file in Azure platform
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.