Introduction
Sometimes, you do need a number of worksheets with common formattings 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.
Moving or Copying Sheets using MS Excel
Following are the steps involved for copying and moving worksheets within or between workbooks.
1. To move or copy sheets to another workbook, open the workbook that will receive the sheets.
2. Switch to the workbook that contains the sheets you want to move or copy, and then select the sheets.
3. On the Edit menu, click Move or Copy Sheet.
4. In the To book box, click the workbook to receive the sheets.
To move or copy the selected sheets to a new workbook, click new book.
5. In the Before sheet box, click the sheet before which you want to insert the moved or copied sheets.
6. To copy the sheets instead of moving them, select the Create a copy check box.
Copy Worksheets within a Workbook
Aspose.Cells provides an overloaded method Aspose.Cells.Worksheets.AddCopy() used to add a worksheet to the collection and copies data from the existing worksheet to it. One version of the method takes the index of the source worksheet as a parameter. The other version takes the name of the source worksheet as the parameter.
Example:
The following example shows how to add a copy of an exising worksheet within the workbook.
[C#]
//Create a new Workbook.
Workbook wb = new Workbook();
//Open an existing excel file.
wb.Open(@"d:\test\book1.xls");
//Create a Worksheets object with reference to
//the sheets of the Workbook.
Worksheets sheets = wb.Worksheets;
//Copy data to a new sheet from an existing
//sheet within the Workbook.
sheets.AddCopy("MySheet");
//Save the excel file.
wb.Save(@"d:\test\mybook.xls");
[VB]
'Create a new Workbook.
Dim wb As Workbook = New Workbook()
'Open an existing excel file.
wb.Open("d:\test\book1.xls")
'Create a Worksheets object with reference to
'the sheets of the Workbook.
Dim sheets As Worksheets = wb.Worksheets
'Copy data to a new sheet from an existing
'sheet within the Workbook.
sheets.AddCopy("MySheet")
'Save the excel file.
wb.Save("d:\test\mybook.xls")
[JAVA]
//Create a new Workbook.
Workbook wb = new Workbook();
//Open an existing excel file.
wb.open("d:\\test\\book1.xls");
//Create a Worksheets object with reference to
//the sheets of the Workbook.
Worksheets sheets = wb.getWorksheets();
//Copy data to a new sheet from an existing
//sheet within the Workbook.
sheets.addCopy("MySheet");
//Save the excel file.
wb.save("d:\\test\\mybook.xls");
Copy Worksheets between Workbooks
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.
Example1:
The following example shows how to copy a workhseet from one workbook to another workbook.
[C#]
//Create a Workbook.
Workbook excelWorkbook0 = new Workbook();
//Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
//Open a file into the first book.
excelWorkbook0.Open(@"C:\Template1.xls");
//Copy the first sheet of the first book into second book.
excelWorkbook1.Worksheets[0].Copy(excelWorkbook0.Worksheets[0]);
//Save the file.
excelWorkbook1.Save(@"C:\FinalBook.xls", FileFormatType.Default);
[VB]
'Create a Workbook.
Dim excelWorkbook0 As Workbook = New Workbook()
'Create another Workbook.
Dim excelWorkbook1 As Workbook = New Workbook()
'Open a file into the first book.
excelWorkbook0.Open("C:\Template1.xls")
'Copy the first sheet of the first book into second book.
excelWorkbook1.Worksheets(0).Copy(excelWorkbook0.Worksheets(0))
'Save the file.
excelWorkbook1.Save("C:\FinalBook.xls", FileFormatType.Default)
[JAVA]
//Create a Workbook.
Workbook excelWorkbook0 = new Workbook();
//Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
//Open a file into the first book.
excelWorkbook0.open("C:\\Template1.xls");
//Copy the first sheet of the first book into second book.
excelWorkbook1.getWorksheets().getSheet(0).copy(excelWorkbook0.getWorksheets().getSheet(0));
//Save the file.
excelWorkbook1.save("C:\\FinalBook.xls", FileFormatType.DEFAULT);
Example2:
The following example shows how to copy a workhseet from one workbook to another workbook.
[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.
Aspose.Cells.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", FileFormatType.Default);
[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 Aspose.Cells.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", FileFormatType.Default)
[JAVA]
//Create a new Workbook.
Workbook excelWorkbook0 = new Workbook();
//Get the first worksheet in the book.
Worksheet ws0 = excelWorkbook0.getWorksheets().getSheet(0);
//Put some data into header rows (A1:A4)
for (int i = 0; i < 5; i++)
{
ws0.getCells().getCell(i, 0).setValue("Header Row " + i);
}
//Put some detail data (A5:A999)
for (int i = 5; i < 1000; i++)
{
ws0.getCells().getCell(i, 0).setValue("Detail Row " + i);
}
//Define a pagesetup object based on the first worksheet.
PageSetup pagesetup = ws0.getPageSetup();
//The first five rows are repeated in each page...
//It can be seen in print preview.
pagesetup.setPrintTitleRows("$1:$5");
//Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
//Get the first worksheet in the book.
Worksheet ws1 = excelWorkbook1.getWorksheets().getSheet(0);
//Name the worksheet.
ws1.setName("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\\ccopyworksheet.xls", FileFormatType.DEFAULT);
Move Worksheets within Workbook
Aspose.Cells provides a method Aspose.Cells.Worksheet.Move() used to move a worksheet to another location in the spreadsheet. The method takes the target worksheet index as a parameter.
Example:
The following example shows how to move a worksheet to another location within the workbook.
[C#]
//Create a new Workbook.
Workbook wb = new Workbook();
//Open an existing excel file.
wb.Open(@"d:\test\BkFinance.xls");
//Create a Worksheets object with reference to
//the sheets of the Workbook.
Worksheets sheets = wb.Worksheets;
//Get the first worksheet.
Worksheet worksheet = sheets[0];
//Move the first sheet to the third position in the workbook.
worksheet.Move(2);
//Save the excel file.
wb.Save(@"d:\test\BkFinance.xls");
[VB]
'Create a new Workbook.
Dim wb As Workbook = New Workbook()
'Open an existing excel file.
wb.Open("d:\test\BkFinance.xls")
'Create a Worksheets object with reference to
'the sheets of the Workbook.
Dim sheets As Worksheets = wb.Worksheets
'Get the first worksheet.
Dim worksheet As Worksheet = sheets(0)
'Move the first sheet to the third position in the workbook.
worksheet.Move(2)
'Save the excel file.
wb.Save("d:\test\BkFinance.xls")
[JAVA]
//Create a new Workbook.
Workbook wb = new Workbook();
//Open an existing excel file.
wb.open("d:\\test\\BkFinance.xls");
//Create a Worksheets object with reference to
//the sheets of the Workbook.
Worksheets sheets = wb.getWorksheets();
//Move the first sheet to the third position in the workbook.
sheets.moveSheet(0,2);
//Save the excel file.
wb.save("d:\\test\\BkFinance.xls");