Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active July 9, 2021 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/719efcb90f9408835ed72a0d8625d033 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/719efcb90f9408835ed72a0d8625d033 to your computer and use it in GitHub Desktop.
Copy or Move Worksheets in Excel Files using Java
// Load the Excel file using its path
Workbook wb = new Workbook("book1.xls");
// Access the worksheets in the workbook
WorksheetCollection sheets = wb.getWorksheets();
// Create a copy of the worksheet
sheets.addCopy("Sheet1");
// Save the updated Excel file
wb.save("updated.xls");
// Load the source Excel workbook
Workbook source = new Workbook("source.xls");
// Load the destination Excel workbook
Workbook destination = new Workbook("destination.xls");
// Copy the first sheet of the source workbook into second workbook
destination.getWorksheets().get(0).copy(source.getWorksheets().get(0));
// Save the file.
destination.save("updated.xls", FileFormatType.EXCEL_97_TO_2003);
// Load the Excel file
Workbook wb = new Workbook("workbook.xls");
// Get the first worksheet in the workbook
Worksheet sheet = wb.getWorksheets().get(0);
// Move the first sheet to the third position in the workbook
sheet.moveTo(2);
// Save the updated Excel file
wb.save("updated.xls");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment