Merge Files
Contents
[
Hide
]
Introduction
Aspose.Cells provides different ways for merging files. For simple files with data, formatting, and formulas, the Workbook.combine() method can be used to combine several workbooks, and the Worksheet.copy() method can be used to copy worksheets into a new workbook. These methods are easy to use and effective, but if you have a lot of files to merge, you might find that they take a lot of system resources. To avoid this, use the CellsHelper.mergeFiles static method, a more efficient way to merge several files.
Merge Files Using Aspose.Cells
The following sample code illustrates how to merge large files using the CellsHelper.mergeFiles method. It takes two simple but large files, MyBook1.xls and MyBook2.xls. The files contain formatted data and formulas only.
The CellsHelper.mergeFiles method only supports merging data, styles, formatting, and formulas. Objects like charts, pictures, comments or other objects might not be merged using this method. Moreover, a cached file is used to store temporary data for the process.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(MergeFiles.class) + "CellsHelperClass/"; | |
// Create an Array (length=2) | |
String[] files = new String[2]; | |
// Specify files with their paths to be merged | |
files[0] = dataDir + "Book1.xls"; | |
files[1] = dataDir + "Book2.xls"; | |
// Create a cachedFile for the process | |
String cacheFile = dataDir + "test.txt"; | |
// Output File to be created | |
String dest = dataDir + "MergeFiles_out.xls"; | |
// Merge the files in the output file | |
CellsHelper.mergeFiles(files, cacheFile, dest); | |
// Now if you need to rename your sheets, you may load the output file | |
Workbook workbook = new Workbook(dataDir + "MergeFiles_out.xls"); | |
int cnt = 1; | |
// Browse all the sheets to rename them accordingly | |
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) { | |
workbook.getWorksheets().get(i).setName("Sheet1" + cnt); | |
cnt++; | |
} | |
// Re-save the file | |
workbook.save(dataDir + "MergeFiles1_out.xls"); |