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 a chart in the same worksheet?
When a chart with the same formatting is to be applied for different charts, there is no need to create 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 repositioning the chart in the worksheet.
To copy and insert a chart in the same worksheet, you need to follow the steps below.
Steps to copy and insert a chart
- Create a workbook and add chart data to 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); // Enter 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)
'Enter 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.

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to copy and insert a chart in the same worksheet using 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, Support Tickets, or feedback portal. We are always happy to assist you!