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

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.
//your code here...//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")

Url: http://www.aspose.com/.net/excel-component.aspx

Language: C# | User: Sheraz Khan | Created: Sep 23, 2015 | Tags: Copy Shapes between Worksheets Create a workbook object Copy a Chart from one Worksheet to Another Copy picture between worksheets .NET Excel API Copy controls b/t worksheet to another