Copy or Move Worksheets in Excel Files using Java

Copy Worksheets in Excel Files using Java

You may often need to copy worksheets within an Excel workbook or from one workbook to another. Also, changing the position of the worksheets is also required in certain cases. In order to perform the above-mentioned tasks programmatically, this article shows how to copy worksheets within Excel workbooks using Java. Furthermore, it also covers how to move a worksheet in an Excel workbook.

Java API to Copy or Move Excel Worksheets

In order to copy or move the worksheets in Excel files, we’ll use Aspose.Cells for Java. It is a feature-rich API that lets you create or modify spreadsheet documents quite easily. You can either download the API or install it using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-cells</artifactId>
    <version>21.6</version>
</dependency>

Copy Worksheets within an Excel Workbook in Java

The following are the steps to copy a worksheet in an Excel workbook using Java.

  1. Load Excel file using Workbook class.
  2. Retrieve worksheets in a WorksheetCollection object using Workbook.getWorksheets() method.
  3. Use WorksheetCollection.addCopy(String) method to copy the worksheet by providing its name.
  4. Save the updated workbook using Workbook.save(String) method.

The following code sample shows how to copy or clone a worksheet in an Excel workbook.

Copy Worksheets from One Workbook to Another

You can also copy a worksheet from one Excel workbook to another. The following are the steps to make a clone of a worksheet in one workbook and add it to another.

  1. Create an object of the Workbook class and load the source Excel file.
  2. Create another object of the Workbook class and load the destination Excel file.
  3. Copy worksheet from source to destination workbook using Workbook.getWorksheets().get(int).copy(sourceWorkbook.getWorksheets().get(int)) method.
  4. Save the destination workbook using Workbook.save(String) method.

The following code sample shows how to clone a worksheet from one Excel workbook to another in Java.

Move Worksheets from One Position to Another in Java

The following are the steps to move a worksheet from one position to another in Java.

  1. Load Excel file using Workbook class.
  2. Retrieve worksheets in a WorksheetCollection object using Workbook.getWorksheets() method.
  3. Access the desired worksheet from worksheet collection into a Worksheet object.
  4. Use Worksheet.moveTo(int) method to move the worksheet to the specified index.
  5. Save the updated workbook using Workbook.save(string) method.

The following code sample shows how to move a worksheet from one position to another in Java.

Get a Free API License

You can try and evaluate Aspose.Cells for Java for free by requesting a temporary license.

Conclusion

In this article, you have learned how to copy worksheets in Excel workbooks using Java. Furthermore, you have seen how to move a worksheet from one position to another within an Excel workbook. You can explore other features of Aspose.Cells for Java using the documentation. In case you would have any queries, feel free to post to our forum.

See Also