New to Telerik Document Processing? Download free 30-day trial

Merge Xlsx files in a single Workbook

Product Version Product Author
RadSpreadProcessing 2021.1.113 Dimitar Karamfilov

Description

You have a multiple Xlsx files and you want to merge them in single file.

Solution

You can iterate all files, import them, and copy their worksheets in a new Workbook.

Merge multiple worksheet in a single Workbook

List<string> files = new List<string>(); 
files.Add("Book1.xlsx"); 
files.Add("Book2.xlsx"); 
 
var provider = new XlsxFormatProvider(); 
 
var workbook = new Workbook(); 
 
foreach (string fileName in files) 
{ 
    var newSheet = workbook.Worksheets.Add(); 
    newSheet.Name = fileName; 
    var currentFile = File.ReadAllBytes(fileName); 
    var importedXlsx = provider.Import(currentFile); 
    newSheet.CopyFrom(importedXlsx.Worksheets[0]); 
} 
 
var resultXlsx = provider.Export(workbook); 
File.WriteAllBytes("result.xlsx", resultXlsx); 
In this article