Merge Files

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, Book1.xls and Book2.xls. The files contain formatted data and formulas only.

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