How to copy and insert a chart in the same worksheet using C#,VB.NET?
This article explains how to copy and insert a chart in the same worksheet in XlsIO using C#/VB.NET.
How to copy and insert chart in same worksheet?
When a chart with same formatting is to be applied for different charts, there is no need of creating a new chart every time. Instead, we can copy the existing chart and make use of the cloned chart. This can be achieved by cloning and repositioned the chart in the worksheet.
To copy and insert a chart in same worksheet, you need to follow the below steps.
Steps to copy and insert a chart
- Create a workbook and add chart data in it.
//Create a workbook IWorkbook workbook = application.Workbooks.Create(1); IWorksheet worksheet = workbook.Worksheets[0]; //Add chart data object[] Yvalues = new object[] { 2000, 1000, 1000 }; object[] Xvalues = new object[] { "Total Income", "Expenses", "Profit" };
- Add a chart in the worksheet.
//Create chart IChartShape chart = worksheet.Charts.Add(); chart.Name = "Original"; //Set positions chart.TopRow = 1; chart.LeftColumn = 1; chart.RightColumn = 6; chart.BottomRow = 10; //Set chart type IChartSerie serie = chart.Series.Add(ExcelChartType.Pie); //Enters the X and Y values directly serie.EnteredDirectlyValues = Yvalues; serie.EnteredDirectlyCategoryLabels = Xvalues;
The below screenshot shows output of the chart created using the above code.
- Then clone the chart added and set the positions for the new chart.
//Copying the existing chart IChartShape chartCopy = (IChartShape)(chart as ChartShapeImpl).Clone(chart.Parent); chartCopy.Name = "Copied"; //Set positions chartCopy.TopRow = 1; chartCopy.LeftColumn = 8; chartCopy.RightColumn = 13; chartCopy.BottomRow = 10;
- Add data for the new chart.
//Add new chart data Yvalues = new object[] { 3500, 2500, 1000 }; Xvalues = new object[] { "Total Income", "Expenses", "Profit" }; chartCopy.Series[0].EnteredDirectlyValues = Yvalues; chartCopy.Series[0].EnteredDirectlyCategoryLabels = Xvalues;
- Save the workbook.
//Save and close the workbook Stream outStream = File.Create("Output.xlsx"); workbook.SaveAs(outStream);
To know more about working with charts in XlsIO, please refer the documentation.
The following C#/VB.NET complete code snippet shows how to copy and insert a chart in the same worksheet in XlsIO.
using Syncfusion.XlsIO; using Syncfusion.XlsIO.Implementation.Shapes; using System.Drawing; using System.IO; using System.Reflection; namespace XlsIO_Sample { class Program { public static void Main(string[] args) { //Instantiate the spreadsheet creation engine using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; //Create a workbook IWorkbook workbook = application.Workbooks.Create(1); IWorksheet worksheet = workbook.Worksheets[0]; //Add chart data object[] Yvalues = new object[] { 2000, 1000, 1000 }; object[] Xvalues = new object[] { "Total Income", "Expenses", "Profit" }; //Create chart IChartShape chart = worksheet.Charts.Add(); chart.ChartTitle = "Original"; //Set positions chart.TopRow = 1; chart.LeftColumn = 1; chart.RightColumn = 6; chart.BottomRow = 10; //Set chart type IChartSerie serie = chart.Series.Add(ExcelChartType.Pie); //Enters the X and Y values directly serie.EnteredDirectlyValues = Yvalues; serie.EnteredDirectlyCategoryLabels = Xvalues; //Copying the existing chart IChartShape chartCopy = (IChartShape)(chart as ChartShapeImpl).Clone(chart.Parent); chartCopy.ChartTitle = "Copied"; //Set positions chartCopy.TopRow = 1; chartCopy.LeftColumn = 8; chartCopy.RightColumn = 13; chartCopy.BottomRow = 10; //Add new chart data Yvalues = new object[] { 3500, 2500, 1000 }; Xvalues = new object[] { "Total Income", "Expenses", "Profit" }; chartCopy.Series[0].EnteredDirectlyValues = Yvalues; chartCopy.Series[0].EnteredDirectlyCategoryLabels = Xvalues; //Save and close the workbook Stream outStream = File.Create("Output.xlsx"); workbook.SaveAs(outStream); } } } }
Imports Syncfusion.XlsIO Imports Syncfusion.XlsIO.Implementation.Shapes Imports System.Drawing Imports System.IO Imports System.Reflection Namespace XlsIO_Sample Class Program Public Shared Sub Main(ByVal args As String()) 'Instantiate the spreadsheet creation engine Using excelEngine As ExcelEngine = New ExcelEngine() Dim application As IApplication = excelEngine.Excel 'Create a workbook Dim workbook As IWorkbook = application.Workbooks.Create(1) Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Add chart data Dim Yvalues As Object() = New Object() {2000, 1000, 1000} Dim Xvalues As Object() = New Object() {"Total Income", "Expenses", "Profit"} 'Create chart Dim chart As IChartShape = worksheet.Charts.Add() chart.ChartTitle = "Original" 'Set positions chart.TopRow = 1 chart.LeftColumn = 1 chart.RightColumn = 6 chart.BottomRow = 10 'Set chart type Dim serie As IChartSerie = chart.Series.Add(ExcelChartType.Pie) 'Enters the X and Y values directly serie.EnteredDirectlyValues = Yvalues serie.EnteredDirectlyCategoryLabels = Xvalues 'Copy the existing chart Dim chartCopy As IChartShape = CType((TryCast(chart, ChartShapeImpl)).Clone(chart.Parent), IChartShape) chartCopy.ChartTitle = "Copied" 'Set positions chartCopy.TopRow = 1 chartCopy.LeftColumn = 8 chartCopy.RightColumn = 13 chartCopy.BottomRow = 10 'Add New chart data Yvalues = New Object() {3500, 2500, 1000} Xvalues = New Object() {"Total Income", "Expenses", "Profit"} chartCopy.Series(0).EnteredDirectlyValues = Yvalues chartCopy.Series(0).EnteredDirectlyCategoryLabels = Xvalues 'Save and close the workbook Dim outStream As Stream = File.Create("Output.xlsx") workbook.SaveAs(outStream) End Using End Sub End Class End Namespace
The below screenshot shows the output of the generated Excel file after inserting the chart in XlsIO.