How to convert the chart created from external data range using XlsIO?
This article explains how to convert chart created with external data range using XlsIO in C#/VB.NET.
What is chart with external data?
The chart created with the data from an external workbook is called chart with external data. The chart from the current worksheet holds a chart cache to maintain the chart data. For conversion, ExcelChartToImageConverter uses that cache information when the chart is created with external data range. By default, XlsIO does not parse the chart cache information. So, we must set the IsChartCacheEnabled property to true in IApplication to parse that cache information before opening the excel file. After parsing the cache information, it is used for converting the chart properly.
To convert chart created with external data range, you need to follow the below steps.
Steps to convert chart with external data range
- Enable the chart cache parsing to parse the data from the chart.
//Enable the chart cache parsing application.IsChartCacheEnabled = true;
- Instantiate ChartToImageConverter in IApplication and set the scaling mode for the chart while conversion.
//Instantiate chart to image converter application.ChartToImageConverter = new ChartToImageConverter(); application.ChartToImageConverter.ScalingMode = ScalingMode.Best;
- Convert the workbook to PDF and saving the pdf file.
//Open the Excel Document to Convert ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook); //Converting workbook to PDF PdfDocument doc = converter.Convert(); //Saving the PDF document Stream stream = File.Create("Output.pdf"); doc.Save(stream);
To know more about chart conversion using XlsIO, please refer the documentation.
The following C#/VB.NET complete code snippet shows how to convert chart with external data range.
using Syncfusion.ExcelChartToImageConverter; using Syncfusion.ExcelToPdfConverter; using Syncfusion.Pdf; using Syncfusion.XlsIO; using System.IO; using System.Reflection; namespace XlsIO_Sample { class Program { static void Main(string[] args) { using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; //Enable the chart cache parsing application.IsChartCacheEnabled = true; //Instantiate chart to image converter application.ChartToImageConverter = new ChartToImageConverter(); application.ChartToImageConverter.ScalingMode = ScalingMode.Best; IWorkbook workbook; //Open existing workbook with data entered Assembly assembly = typeof(Program).GetTypeInfo().Assembly; Stream fileStream = assembly.GetManifestResourceStream("CSharpSample.Sample.xlsx"); workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic); IChart chart = workbook.Worksheets[0].Charts[0]; IRange range = chart.DataRange; //Open the Excel Document to Convert ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook); //Converting workbook to PDF PdfDocument doc = converter.Convert(); //Saving the PDF document Stream stream = File.Create("Output.pdf"); doc.Save(stream); converter.Dispose(); doc.Close(true); } } } }
Imports Syncfusion.XlsIO Imports Syncfusion.ExcelChartToImageConverter Imports Syncfusion.ExcelToPdfConverter Imports Syncfusion.Pdf Imports System.Reflection Imports System.IO Namespace XlsIO_Sample Module Program Public Sub Main() 'Instantiate the spreadsheet creation engine Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel 'Enable the chart cache parsing application.IsChartCacheEnabled = False 'Instantiate chart to image converter application.ChartToImageConverter = New ChartToImageConverter() application.ChartToImageConverter.ScalingMode = ScalingMode.Best 'Open existing workbook Dim workbook As IWorkbook Dim assembly As Assembly = GetType(Program).GetTypeInfo().Assembly Dim fileStream As Stream = assembly.GetManifestResourceStream("VBSample.Sample.xlsx") workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic) 'Open the Excel Document to Convert Dim converter As New ExcelToPdfConverter(workbook) 'Converting to PDF Dim doc As PdfDocument = converter.Convert() 'Saving the pdf file Dim stream As Stream = File.Create("Output.pdf") doc.Save(stream) converter.Dispose() excelEngine.Dispose() doc.Close(True) End Using End Sub End Module End Namespace
The below screenshot shows the output chart with external data range after conversion.