Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active February 8, 2026 10:01
Show Gist options
  • Select an option

  • Save aspose-com-kb/69068c8ec0eb176835c0a5ed2765cfb9 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-kb/69068c8ec0eb176835c0a5ed2765cfb9 to your computer and use it in GitHub Desktop.
Merge Excel Files using Java. For more details: https://kb.aspose.com/cells/java/merge-excel-files-using-java/
import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import java.util.ArrayList;
import java.util.List;
public class MergeExcelFiles {
public static void main(String[] args) throws Exception {
// License
License lic = new License();
lic.setLicense("license.lic");
// 1. Create a list of Excel files
List<String> excelFiles = new ArrayList<>();
excelFiles.add("File1.xlsx");
excelFiles.add("File2.xlsx");
excelFiles.add("File3.xlsx");
excelFiles.add("File4.xlsx");
excelFiles.add("File5.xlsx");
excelFiles.add("File6.xlsx");
if (excelFiles.isEmpty()) {
System.out.println("No files to combine.");
return;
}
// 2. Load the first workbook as the destination
Workbook combinedWorkbook = new Workbook(excelFiles.get(0));
// 3. Combine remaining workbooks
for (int i = 1; i < excelFiles.size(); i++) {
Workbook tempWorkbook = new Workbook(excelFiles.get(i));
combinedWorkbook.combine(tempWorkbook);
}
// 4. Save the combined workbook
combinedWorkbook.save("CombinedOutput.xlsx");
System.out.println("Excel files combined successfully.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment