Important alert: (current site time 7/16/2013 7:32:18 AM EDT)
 

VB icon

Copy Worksheets with Tables, Charts & Images Within or b/t Workbooks

Email
Submitted on: 10/5/2011 1:55:57 PM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET, ASP.NET
Views: 2245
 
     This technical tip shows how to Copy or Move worksheets within or between workbooks. Sometimes, you do need a number of worksheets with common formatting and data entry. For example, if you work with quarterly budgets, you might want to create a workbook with sheets that contain the same column headings, row headings, and formulas. There is a way to do this: by creating one sheet and then copying it three times. Generally, it's easier to create and copy the worksheets. Aspose.Cells supports Copy or Move worksheets within or between workbooks. The worksheets including data, formattings, tables, matrices, charts, images and other objects are copied with the highest degree of precision. Aspose.Cells provides a method Aspose.Cells.Worksheet.Copy() used to copy data and formatting from a source worksheet to another worksheet within or between the workbooks. The method takes the source worksheet object as a parameter. Example: The following example shows how to copy a worksheet from one workbook to another workbook. More about Aspose.Cells for .NET - Homepage of Aspose.Cells for .NET: http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx - Read More Technical Tips by Aspose.Cells for .NET at: http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/technical-articles.html - Download Aspose.Cells for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/default.aspx Contact Information Aspose Pty Ltd, Suite 163, 79 Longueville Road Lane Cove, NSW, 2066 Australia http://www.aspose.com/ sales@aspose.com Phone: 888.277.6734 Fax: 866.810.9465l
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Copy Worksheets with Tables, Charts & Images Within or b/t Workbooks
// Description:This technical tip shows how to Copy or Move worksheets within or between workbooks. Sometimes, you do need a number of worksheets with common formatting and data entry. For example, if you work with quarterly budgets, you might want to create a workbook with sheets that contain the same column headings, row headings, and formulas. There is a way to do this: by creating one sheet and then copying it three times. Generally, it's easier to create and copy the worksheets. Aspose.Cells supports Copy or Move worksheets within or between workbooks. The worksheets including data, formattings, tables, matrices, charts, images and other objects are copied with the highest degree of precision.
Aspose.Cells provides a method Aspose.Cells.Worksheet.Copy() used to copy data and formatting from a source worksheet to another worksheet within or between the workbooks. The method takes the source worksheet object as a parameter.
Example:
The following example shows how to copy a worksheet from one workbook to another workbook.
More about Aspose.Cells for .NET
- Homepage of Aspose.Cells for .NET: http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx
- Read More Technical Tips by Aspose.Cells for .NET at: http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/technical-articles.html 
- Download Aspose.Cells for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/default.aspx
Contact Information
Aspose Pty Ltd, Suite 163, 
79 Longueville Road
Lane Cove, NSW, 2066 
Australia 
http://www.aspose.com/
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465l
// By: aspose_seo
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8535&lngWId=10//for details.//**************************************

[C#]
 
//Create a new Workbook.
Workbook excelWorkbook0 = new Workbook();
 
//Get the first worksheet in the book.
Worksheet ws0 = excelWorkbook0.Worksheets[0];
 
//Put some data into header rows (A1:A4)
for (int i = 0; i < 5; i++)
{
ws0.Cells[i, 0].PutValue(string.Format("Header Row {0}", i));
}
 
//Put some detail data (A5:A999)
for (int i = 5; i < 1000; i++)
{
ws0.Cells[i, 0].PutValue(string.Format("Detail Row {0}", i));
}
 
//Define a pagesetup object based on the first worksheet. 
PageSetup pagesetup = ws0.PageSetup;
 
//The first five rows are repeated in each page... 
//It can be seen in print preview.
pagesetup.PrintTitleRows = "$1:$5";
 
//Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
 
//Get the first worksheet in the book.
Worksheet ws1 = excelWorkbook1.Worksheets[0];
 
//Name the worksheet.
ws1.Name = "MySheet";
 
//Copy data from the first worksheet of the first workbook into the
//first worksheet of the second workbook.
ws1.Copy(ws0);
 
//Save the excel file.
excelWorkbook1.Save(@"d:\test\copyworksheet.xls");
 
[VB]
 
'Create a new Workbook.
Dim excelWorkbook0 As Workbook = New Workbook()
 
'Get the first worksheet in the book.
Dim ws0 As Worksheet = excelWorkbook0.Worksheets(0)
 
'Put some data into header rows (A1:A4)
Dim i As Integer
For i = 0 To 4 Step 1
 
ws0.Cells(i, 0).PutValue(String.Format("Header Row {0}", i))
Next
 
'Put some detail data (A5:A999)
For i = 5 To 999 Step 1
 
ws0.Cells(i, 0).PutValue(String.Format("Detail Row {0}", i))
Next
 
'Define a pagesetup object based on the first worksheet. 
Dim pagesetup As PageSetup = ws0.PageSetup
 
'The first five rows are repeated in each page... 
'It can be seen in print preview.
pagesetup.PrintTitleRows = "$1:$5"
 
'Create another Workbook.
Dim excelWorkbook1 As Workbook = New Workbook()
 
'Get the first worksheet in the book.
Dim ws1 As Worksheet = excelWorkbook1.Worksheets(0)
 
'Name the worksheet.
ws1.Name = "MySheet"
 
'Copy data from the first worksheet of the first workbook into the
'first worksheet of the second workbook.
ws1.Copy(ws0)
 
'Save the excel file.
excelWorkbook1.Save("d:\test\copyworksheet.xls")


Other 16 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 code (in the Intermediate category)?
(The code 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 code, please click here instead.)
 

To post feedback, first please login.