دمج الملفات

مقدمة

يوفر Aspose.Cells طرقًا مختلفة لدمج الملفات. يمكن استخدام الـ Workbook.combine() لدمج العديد من دفاتر العمل للملفات البسيطة بالبيانات والتنسيق والصيغ، ويمكن استخدام الـ Worksheet.copy لنسخ الصفحات العمل في دفتر عمل جديد. هذه الطرق سهلة الاستخدام وفعالة، ولكن إذا كان هناك الكثير من الملفات لدمجها، قد تجد أنها تستهلك الكثير من موارد النظام. لتجنب ذلك، استخدم الطريقة الثابتة CellsHelper.mergeFiles، وهي طريقة أكثر فاعلية لدمج العديد من الملفات.

دمج الملفات باستخدام Aspose.Cells

الرمز المصدري التالي يوضح كيفية دمج الملفات الكبيرة باستخدام طريقة CellsHelper.mergeFiles. يأخذ الملفان الكبيران البسيطان، MyBook1.xls وMyBook2.xls، واللذان يحتويا على بيانات مُنسَقة وصُيغ فقط.

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