Copy and Move Worksheets Within and Between Workbooks

Copying and Moving Worksheets

This article explains how to use Aspose.Cells to:

Copying a Worksheet within a Workbook

The initial steps are the same for all examples.

  1. Create two workbooks with some data in Microsoft Excel. For the purposes of this example, we created two new workbooks in Microsoft Excel and input some data into the worksheets.
  • FirstWorkbook.xls (3 worksheets)

  • SecondWorkbook.xls (1 worksheet).

    FirstWorkbook.xls

todo:image_alt_text

SecondWorkbook.xls

todo:image_alt_text

  1. Download and install Aspose.Cells:
    1. Download Aspose.Cells for Java.
    2. Unzip it on your development computer. All Aspose components, when installed, work in evaluation mode. The evaluation mode has no time limit and it only injects watermarks into produced documents.
  2. Create a project:
    1. Create a project using a Java editor such as Eclipse or create a simple program using a text editor.
  3. Add a class path:
    1. Extract the Aspose.Cells.jar and dom4j_1.6.1.jar from Aspose.Cells.zip.
    2. Set the classpath of project in Eclipse:
      1. Select your project in Eclipse and click menus Project, then Properties.
      2. Select Java Build Path in the left side of the dialog, then select the Libraries tab,
      3. Click Add JARs or Add External JARs to select Aspose.Cells.jar and dom4j_1.6.1.jar and add them into build paths.
  1. Copy worksheet within a workbook: Below is the code used by to accomplish the task. It copies the worksheet Copy within FirstWorkbook.xls.

Executing the code moves the worksheet named Copy within FirstWorkbook.xls with the new name Last Sheet.

Output file

todo:image_alt_text

Moving a Worksheet with in a Workbook

Below is the code used to accomplish the task.

Executing the code moves the worksheet Move from index 1 to index 2 in FirstWorkbook.xls.

Output file

todo:image_alt_text

Copying a Worksheet between Workbooks

Executing the code copies the worksheet Copy to SecondWorkbook.xls with the new name Sheet2.

Output file

todo:image_alt_text

Moving a Worksheet between Workbooks

Executing the code moves the move worksheet from FirstWorkbook.xls to SecondWorkbook.xls with the new name Sheet3.

Output FirstWorkbook.xls

todo:image_alt_text

Output SecondWorkbook.xls

todo:image_alt_text

Conclusion