دمج الملفات

مقدمة

توفر Aspose.Cells طرقًا مختلفة لدمج الملفات. يمكن استخدام الطريقة Workbook.Combine() لدمج عدة دفاتر عمل بيانات وتنسيقات وصيغ، كما يمكن استخدام الطريقة Worksheet.Copy() لنسخ أوراق العمل إلى دفتر عمل جديد. تُستخدم هذه الطرق بسهولة وهي فعالة، ولكن إذا كنت تمتلك الكثير من الملفات لدمجها، قد تجد أنها تستهلك الكثير من موارد النظام. لتجنب ذلك، استخدم الطريقة الثابتة CellsHelper.MergeFiles، وهي الطريقة الأكثر كفاءة لدمج العديد من الملفات.

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

يوضح الكود العيني التالي كيفية دمج الملفات الكبيرة باستخدام الطريقة CellsHelper.MergeFiles. يأخذ ملفين بسيطين ولكن كبيرين فقط، Book1.xls و Book2.xls. يتضمن الملفات بيانات مهيأة وصيغ فقط.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// 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 + "output.xlsx";
// Merge the files in the output file. Supports only .xls files
CellsHelper.MergeFiles(files, cacheFile, dest);
// Now if you need to rename your sheets, you may load the output file
Workbook workbook = new Workbook(dataDir + "output.xlsx");
int i = 1;
// Browse all the sheets to rename them accordingly
foreach (Worksheet sheet in workbook.Worksheets)
{
sheet.Name = "Sheet1" + i.ToString();
i++;
}
// Re-save the file
workbook.Save(dataDir + "output.xlsx");