ファイルの結合

紹介

Aspose.Cellsはファイルをマージするさまざまな方法を提供します。データ、フォーマット、数式が含まれるシンプルなファイルを結合するには Workbook.combine() メソッドを使用し、Worksheet.copy()メソッドでワークシートを新しいブックにコピーできます。これらの方法は簡単に使え効果的ですが、多くのファイルをマージする場合はシステムリソースを大量に使用する可能性があります。これを避けるために、CellsHelper.mergeFilesの静的メソッドを使用してください。これは複数のファイルを効果的にマージするより効率的な方法です。

Aspose.Cellsを使用してファイルをマージする

次のサンプルコードは、CellsHelper.mergeFiles メソッドを使用して大きなファイルをマージする方法を示しています。単純ですが大きなMyBook1.xlsおよびMyBook2.xlsという2つのファイルを含みます。ファイルには、書式設定されたデータと数式のみが含まれます。

// 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");