Home > Code > C# > How to Copy Shapes, Charts or Controls between Excel Worksheets inside .NET Apps

How to Copy Shapes, Charts or Controls between Excel Worksheets inside .NET Apps

by sher azam   on Sep 23, 2015   Category: C#   |  Views: 1145    |  Points: 25   |  Gold 


This technical tip shows how .NET developers can copy shapes between worksheets inside their .NET applications. Sometimes, you need to copy elements on a worksheet, for example pictures, charts and other drawing objects, between worksheets. Aspose.Cells supports this feature. Charts, images and other objects can be copied with the highest degree of precision. This article shows how to copy different shapes like pictures, charts and other drawing objects using Aspose.Cells. Aspose.Cells offers more flexibility than other solutions and provides outstanding speed, efficiency and reliability. Aspose.Cells has benefited from years of research, design and careful tuning. This article shows how to perform three different tasks:

• Copy a picture from one worksheet to another.
• Copy a chart from one worksheet to another.
• Copy controls & other drawing objects from one worksheet to another.

//The following example shows how to Copy a picture from one worksheet to another:

//[C# Code Sample]

//Create a workbook object
//Open the template file
Workbook workbook = new Workbook(@"C:\Shapes.xls");
//Get the Picture from the "Picture" worksheet.
Aspose.Cells.Drawing.Picture source = workbook.Worksheets["Picture"].Pictures[0];
//Save Picture to Memory Stream
MemoryStream ms = new MemoryStream(source.Data);
//Copy the picture to the Result Worksheet
workbook.Worksheets["Result"].Pictures.Add(source.UpperLeftRow, source.UpperLeftColumn, ms, source.WidthScale, source.HeightScale);
//Save the Worksheet
workbook.Save(@"c:\Shapes.xls");

//[VB.NET Code Sample]

'Create a workbook object
'Open the template file
Dim workbook As New Workbook("C:\Shapes.xls")
'Get the Picture from the "Picture" worksheet.
Dim source As Aspose.Cells.Drawing.Picture = workbook.Worksheets("Picture").Pictures(0)
'Save Picture to Memory Stream
Dim ms As New MemoryStream(source.Data)
'Copy the picture to the Result Worksheet
workbook.Worksheets("Result").Pictures.Add(source.UpperLeftRow, source.UpperLeftColumn, ms, source.WidthScale, source.HeightScale)
'Save the Worksheet
workbook.Save("c:\Shapes.xls")

//Copy a Chart from one Worksheet to Another

//[C# Code Sample]

//Create a workbook object
//Open the template file
Workbook workbook = new Workbook(@"C:\Shapes.xls");
//Get the Chart from the "Chart" worksheet.
Aspose.Cells.Charts.Chart source = workbook.Worksheets["Chart"].Charts[0];
Aspose.Cells.Drawing.ChartShape cshape = source.ChartObject;
//Copy the Chart to the Result Worksheet
workbook.Worksheets["Result"].Shapes.AddCopy(cshape, 20, 0, 2, 0);
//Save the Worksheet
workbook.Save(@"c:\Shapes.xls");

//[VB.NET Code Sample]

'Create a workbook object
'Open the template file
Dim workbook As New Workbook("C:\Shapes.xls")
'Get the Chart from the "Chart" worksheet.
Dim source As Aspose.Cells.Charts.Chart = workbook.Worksheets("Chart").Charts(0)
Dim cshape As Aspose.Cells.Drawing.ChartShape = source.ChartObject
'Copy the Chart to the Result Worksheet
workbook.Worksheets("Result").Shapes.AddCopy(cshape, 20, 0, 2, 0)
'Save the Worksheet
workbook.Save("c:\Shapes.xls")

//Copy Controls and Other Drawing Objects from One Worksheet to Another

//[C# Code Sample]

//Create a workbook object
//Open the template file
Workbook workbook = new Workbook(@"C:\Controls.xls");
//Get the Shapes from the "Control" worksheet.
Aspose.Cells.Drawing.ShapeCollection shape = workbook.Worksheets["Control"].Shapes;
//Copy the Textbox to the Result Worksheet
workbook.Worksheets["Result"].Shapes.AddCopy(shape[0], 5, 0, 2, 0);
//Copy the Oval Shape to the Result Worksheet
workbook.Worksheets["Result"].Shapes.AddCopy(shape[1], 10, 0, 2, 0);
//Save the Worksheet
workbook.Save(@"C:\Controls.xls");

//[VB.NET Code Sample]

'Create a workbook object
'Open the template file
Dim workbook As New Workbook("C:\Controls.xls")
'Get the Shapes from the "Control" worksheet.
Dim shape As Aspose.Cells.Drawing.ShapeCollection = workbook.Worksheets("Control").Shapes
'Copy the Textbox to the Result Worksheet
workbook.Worksheets("Result").Shapes.AddCopy(shape(0), 5, 0, 2, 0)
'Copy the Oval Shape to the Result Worksheet
workbook.Worksheets("Result").Shapes.AddCopy(shape(1), 10, 0, 2, 0)
'Save the Worksheet
workbook.Save("C:\Controls.xls")


More about Aspose.Cells for .NET

- Homepage of Aspose.Cells for .NET: http://www.aspose.com/.net/excel-component.aspx

- Download Aspose.Cells for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts