Articles in this section
Category / Section

How to merge multiple CSV files into single Excel workbook

1 min read

Syncfusion XlsIO do support merging different Excel files into a single Excel document. This article explains how to merge multiple CSV documents into a single Excel document.

This can be achieved through AddCopy functionality available in XlsIO. The following complete code snippet explains this.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
        IApplication application = excelEngine.Excel;
        application.DefaultVersion = ExcelVersion.Excel2016;
 
        string[] CSVFiles = new string[] { "CSVFile1", "CSVFile2", "CSVFile3" };
 
        IWorkbook workbook = application.Workbooks.Create(1);
 
        for (int i = 0; i < CSVFiles.Length; i++)
        {
        FileStream inputStream = new FileStream(CSVFiles[i] + ".csv", FileMode.Open, FileAccess.Read);
        IWorkbook CSVWorkbook = application.Workbooks.Open(inputStream);
        workbook.Worksheets.AddCopy(CSVWorkbook.Worksheets);
        }
 
        workbook.Worksheets[0].Remove();
 
        //Saving the Excel to the MemoryStream 
        MemoryStream stream = new MemoryStream();
        workbook.SaveAs(stream);
 
        //Set the position as '0'.
        stream.Position = 0;
 
        //Download the Excel file in the browser
        FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/excel");
        fileStreamResult.FileDownloadName = "Output.xlsx";
        return fileStreamResult;
}

 

A complete working sample can be downloaded from MergeMultipleCSV.zip.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied