Copying and Moving Worksheets

How to Move or Copy 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 checkbox.

How to Copy Worksheets within a Workbook with Aspose.Cells for Python Excel Library

Aspose.Cells for Python via .NET provides an overloaded method, Aspose.Cells.WorksheetCollection.add_copy(), that is used to add a worksheet to the collection and copy 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.

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

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
InputPath = dataDir + "book1.xls"
# Open an existing Excel file.
wb = Workbook(InputPath)
# Create a Worksheets object with reference to
# the sheets of the Workbook.
sheets = wb.worksheets
# Copy data to a new sheet from an existing
# sheet within the Workbook.
sheets.add_copy("Sheet1")
# Save the Excel file.
wb.save(dataDir + "CopyWithinWorkbook_out.xls")

How to Copy Worksheets between Workbooks

Aspose.Cells for Python via .NET 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.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
InputPath = dataDir + "book1.xls"
# Create a Workbook.
# Open a file into the first book.
excelWorkbook0 = Workbook(InputPath)
# Create another Workbook.
excelWorkbook1 = Workbook()
# Copy the first sheet of the first book into second book.
excelWorkbook1.worksheets[0].copy(excelWorkbook0.worksheets[0])
# Save the file.
excelWorkbook1.save(dataDir + "CopyWorksheetsBetweenWorkbooks_out.xls")

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

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create a new Workbook.
excelWorkbook0 = Workbook()
# Get the first worksheet in the book.
ws0 = excelWorkbook0.worksheets[0]
# Put some data into header rows (A1:A4)
for i in range(5):
headerRow = "Header Row " + str(i)
ws0.cells.get(i, 0).put_value(headerRow)
# Put some detail data (A5:A999)
for i in range(5, 1000):
detailRow = "Detail Row " + str(i)
ws0.cells.get(i, 0).put_value(detailRow)
# Define a pagesetup object based on the first worksheet.
pagesetup = ws0.page_setup
# The first five rows are repeated in each page...
# It can be seen in print preview.
pagesetup.print_title_rows = "$1:$5"
# Create another Workbook.
excelWorkbook1 = Workbook()
# Get the first worksheet in the book.
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(dataDir + "CopyWorksheetFromWorkbookToOther_out.xls")

How to Move Worksheets within Workbook

Aspose.Cells for Python via .NET provides a method Aspose.Cells.Worksheet.move_to() which is used to move a worksheet to another location in the same 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.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
InputPath = dataDir + "book1.xls"
# Open an existing excel file.
wb = Workbook(InputPath)
# Create a Worksheets object with reference to
# the sheets of the Workbook.
sheets = wb.worksheets
# Get the first worksheet.
worksheet = sheets[0]
# Move the first sheet to the third position in the workbook.
worksheet.move_to(2)
# Save the excel file.
wb.save(dataDir + "MoveWorksheet_out.xls")