Copy fragments and worksheets between Workbooks

Sometimes, you do need a number of worksheets with common formatting and data. 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.
Aspose.Cells supports copying and moving worksheets within or between workbooks. Worksheet, complete with data, formatting, tables, matrices, charts, images and other objects, are copied with the highest degree of precision.

Moving or Copying Sheets using Microsoft Excel
Following are the steps involved for copying and moving worksheets within or between workbooks in Microsoft Excel.

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 dialog, click the workbook to receive the sheets.
5) To move or copy the selected sheets to a new workbook, click New Book.
6) In the Before sheet box, click the sheet before which you want to insert the moved or copied sheets.
7) To copy the sheets instead of moving them, select the Create a copy check box.

Copy Worksheets within a Workbook with Aspose.Cells

Aspose.Cells provides an overloaded method, Aspose.Cells.WorksheetCollection.AddCopy(), that is used to add a worksheet to the collection and copies data from an existing worksheet. 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.

The following example shows how to copy an existing worksheet within a workbook.

//Create a new Workbook.
            //Open an existing Excel file.
            Workbook wb = new Workbook("ResultedBook.xls");

            //Create a Worksheets object with reference to
            //the sheets of the Workbook.
            WorksheetCollection 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("Copy Worksheet.xls");

Download

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 workbooks. The method takes the source worksheet object as a parameter.

The following example shows how to copy a worksheet from one workbook to another workbook.

//Create a Workbook.
            //Open a file into the first book.
            Workbook excelWorkbook0 = new Workbook("WorkBook Operations.xls");

            //Create another Workbook.
            Workbook excelWorkbook1 = new Workbook();

            //Copy the first sheet of the first book into second book.
            excelWorkbook1.Worksheets[0].Copy(excelWorkbook0.Worksheets[0]);

            //Save the file.
            excelWorkbook1.Save("ResultedBook.xls");

Download

The following example shows how to copy a worksheet from one workbook to another workbook.

//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("copyworksheet.xls");
 

Download

Move Worksheets within Workbook

Aspose.Cells provides a method, Aspose.Cells.Worksheet.MoveTo(), used to move a worksheet to another location in the spreadsheet. The method takes the target worksheet index as a parameter.

The following example shows how to move a worksheet to another location within the workbook.

//Create a new Workbook.
            //Open an existing excel file.
            Workbook wb = new Workbook("WorkBook Operations.xls");

            //Create a Worksheets object with reference to
            //the sheets of the Workbook.
            WorksheetCollection sheets = wb.Worksheets;

            //Get the first worksheet.
            Worksheet worksheet = sheets[0];
            string test=worksheet.Name;
            //Move the first sheet to the third position in the workbook.
            worksheet.MoveTo(2);

            //Save the excel file.
            wb.Save("Move worksheets within workbook.xls");

Download

Last edited Mar 3, 2014 at 6:39 AM by asposemarketplace, version 3