Articles in this section
Category / Section

How to merge multiple CSV files into single Excel workbook

1 min read

Syncfusion XlsIO does 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 the 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.

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

 

Conclusion

I hope you enjoyed learning about How to merge multiple CSV files into single Excel workbook.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion&reg; components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

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