Copying and Moving Worksheets

Moving or Copying Sheets using Microsoft 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.
  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.

Copy Worksheets within a Workbook

Aspose.Cells provides an overloaded method, WorksheetCollection.addCopy(), 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.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/";
// Create a new Workbook by excel file path
Workbook wb = new Workbook(dataDir + "book1.xls");
// Create a Worksheets object with reference to the sheets of the Workbook.
WorksheetCollection sheets = wb.getWorksheets();
// Copy data to a new sheet from an existing sheet within the Workbook.
sheets.addCopy("Sheet1");
// Save the excel file.
wb.save(dataDir + "CopyWithinWorkbook_out.xls");

Copy Worksheets between Workbooks

Aspose.Cells provides a method, 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.

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/";
// Create a Workbook.
Workbook excelWorkbook0 = new Workbook(dataDir + "book1.xls");
// Create another Workbook.
Workbook excelWorkbook1 = new Workbook();
// Copy the first sheet of the first book into second book.
excelWorkbook1.getWorksheets().get(0).copy(excelWorkbook0.getWorksheets().get(0));
// Save the file.
excelWorkbook1.save(dataDir + "CWBetweenWorkbooks_out.xls", FileFormatType.EXCEL_97_TO_2003);

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(AddingPageBreaks.class) + "worksheets/";
// Create a new Workbook.
Workbook excelWorkbook0 = new Workbook();
// Get the first worksheet in the book.
Worksheet ws0 = excelWorkbook0.getWorksheets().get(0);
// Put some data into header rows (A1:A4)
for (int i = 0; i < 5; i++) {
ws0.getCells().get(i, 0).setValue("Header Row " + i);
}
// Put some detail data (A5:A999)
for (int i = 5; i < 1000; i++) {
ws0.getCells().get(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().get(0);
// Name the worksheet.
ws1.setName("Sheet1");
// 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", FileFormatType.EXCEL_97_TO_2003);

Move Worksheets within Workbook

Aspose.Cells provides a method, Worksheet.moveTo(), 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.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(MoveWorksheet.class) + "worksheets/";
// Create a new Workbook.
Workbook wb = new Workbook(dataDir + "BkFinance.xls");
// Get the first worksheet in the book.
Worksheet sheet = wb.getWorksheets().get(0);
// Move the first sheet to the third position in the workbook.
sheet.moveTo(2);
// Save the Excel file.
wb.save(dataDir + "MoveWorksheet_out.xls");