Print a chart in Excel using C# and VB.NET
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can print a chart in Excel document using C# and VB.NET.
Steps to print a chart in Excel, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application
Step 2: Install Syncfusion.ExcelToPdfConverter.WPF and Syncfusion.ExcelChartToImageConverter.WPF NuGet packages as reference to your .NET Framework application from NuGet.org.
Install ExcelChartToImageConverter NuGet package
Install ExcelToPdfConverter NuGet package
Step 3: Include the following namespaces in the Program.cs file.
C#
using Syncfusion.ExcelChartToImageConverter; using Syncfusion.ExcelToPdfConverter; using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.ExcelChartToImageConverter Imports Syncfusion.ExcelToPdfConverter Imports Syncfusion.XlsIO
Step 4: Use the following code snippet to print a chart in Excel using C# and VB.NET.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the application object IApplication application = excelEngine.Excel; //Initialize chart to image converter application.ChartToImageConverter = new ChartToImageConverter(); application.ChartToImageConverter.ScalingMode = ScalingMode.Best; //Open the sample Excel workbook that contains chart IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic); //Accessing the first worksheet IWorksheet worksheet = workbook.Worksheets[0]; //Accessing the chart to print IChart chart = worksheet.Charts[0]; //Print the chart ExcelToPdfConverter converter = new ExcelToPdfConverter(chart); converter.Print(); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the application object Dim application As IApplication = excelEngine.Excel 'Initialize chart to image converter application.ChartToImageConverter = New ChartToImageConverter() application.ChartToImageConverter.ScalingMode = ScalingMode.Best 'Open the sample Excel workbook that contains chart Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic) 'Accessing the first worksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Accessing the chart to print Dim chart As IChart = worksheet.Charts(0) 'Print the chart Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(chart) converter.Print() End Using
A complete working example of how to print a chart in Excel using C# and VB.NET can be downloaded from Print Excel charts.zip.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
See Also:
How to convert Excel to PDF in C#, VB.NET
How to Export and Save Excel Chart as Image
How to convert Syncfusion’s WinForms chart to Excel chart using XlsIO
How to copy and insert a chart in the same worksheet using C#,VB.NET?
How to create Excel line with markers chart in C#, VB.NET?
Format Excel chart data label in C#, VB.NET
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 the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.