Copying and Moving Worksheets
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. Worksheets, 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
The following are the steps involved in copying and moving worksheets within or between workbooks.
- Open the workbook that will receive the sheets.
- Switch to the workbook that contains the sheets you want to move or copy, and then select the sheets.
- On the Edit menu, click Move or Copy Sheet.
- 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.
- In the Before sheet box, click the sheet before which you want to insert the moved or copied sheets.
- To copy the sheets instead of moving them, select the Create a copy checkbox.
Copy Worksheets within a Workbook
Aspose.Cells provides an overloaded WorksheetCollection.addCopy() method that is used to copy 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.
The following example shows how to copy an existing worksheet within a workbook.
Copy Worksheets between Workbooks
Aspose.Cells provides the Worksheet.copy() method used to copy worksheets to other 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.
Move Worksheets within Workbook
Aspose.Cells provides the Worksheet.moveTo() method used to move a worksheet to another location in the same spreadsheet.
The following example shows how to move a worksheet to another location within the workbook.