How to convert the chart created from external data range using XlsIO?
This article explains how to convert a chart created with an external data range using XlsIO in C#/VB.NET.
What is chart with external data?
A chart created with the data from an external workbook is called a 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 an 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 s chart with an external data range
- Enable the chart cache parsing to parse the data from the chart.
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); } } } }
- 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 a chart with an 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 screenshot below shows the output chart with an external data range after conversion.
Conclusion
I hope you enjoyed learning about how to convert a chart created with an external data range using XlsIO in C#/VB.NET.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!