Copy Worksheets in Excel Files using Java

You may often need to copy sheets in an Excel file 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 Excel sheet in Java. We will explicitly cover how to clone sheets within an Excel workbook or copy a sheet from one workbook to another. And last but not the least, you will learn how to move a worksheet in an Excel workbook.

Java API to Copy Excel Sheets - Free Download

In order to copy or move the sheets 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 Excel Sheets in Java

The following are the steps to copy a worksheet in an Excel workbook in 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 an Excel worksheet in Java.

Copy Excel Worksheets to Another Workbook in Java

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

  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 copy a worksheet from one Excel workbook to another in Java.

Move Excel Sheet’s Position in Java

The following are the steps to move a sheet 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 an Excel sheet from one position to another in Java.

Java Excel Sheets Copier - Get a Free 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 files using Java. Furthermore, you have seen how to move a worksheet from one position to another within an Excel workbook.

Java Excel Library - Read More

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