article

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

Email
Submitted on: 9/23/2015 3:04:20 PM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 3650
 
     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 tutorial 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


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.