Merge Multiple Excel Files into One in Just 3 Steps Using C#
Tags: fileformats, csharp, excel, productivity
Published: 2023-12-08 04:26:28

---
title: Merge Multiple Excel Files into One in Just 3 Steps Using C#
published: true
date: 2023-12-01 10:38:42 UTC
tags: fileformats, csharp, excel, productivity
canonical_url: https://www.syncfusion.com/blogs/post/merge-multiple-excel-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ent0dnsfrae78z3k8e97.png
---
In today’s data-centric world, dealing with related data spread over various Excel documents can be challenging. The traditional method of manually merging these documents into a unified one can be time-consuming and susceptible to errors. But what if there was a more efficient solution?

The [Syncfusion Excel Library](https://www.syncfusion.com/document-processing/excel-framework/net ".NET Excel Library") is a robust tool that facilitates the smooth creation, reading, and editing of Excel documents using C#. This library lets you open and consolidate numerous Excel documents into a single document with just a few lines of code, automating the process, saving valuable time, and ensuring data precision.

Whether navigating through many Excel documents, each holding a fragment of a larger whole, or simply seeking a more efficient data management method, this blog is here to assist you. We will guide you through the steps to merge Excel documents using C#, simplifying your data management tasks.

Let’s get started!

**Note:** If you are new to our Excel Library, following our [Getting Started guide](https://help.syncfusion.com/file-formats/xlsio/getting-started-create-excel-file-csharp-vbnet "Create Excel File in C# and VB.NET") is highly recommended.

## How to merge Excel files with C#

Follow these steps to merge multiple Excel documents into a single file using the Syncfusion Excel Library in C#:

**Step 1:** First, launch [Visual Studio](https://visualstudio.microsoft.com/ "Visual Studio"). Then, [create a new .NET Core console application](https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio?pivots=dotnet-8-0 "Create a .NET console application using Visual Studio"). ![Create a .NET Core Console application](https://www.syncfusion.com/blogs/wp-content/uploads/2023/12/Create-a-.NET-Core-Console-application.png)

**Step 2** : Download and install the most recent version of the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core "Syncfusion.XlsIO.Net.Core NuGet package") NuGet package. ![Install Syncfusion.XlsIO.Net.Core NuGet package.](https://www.syncfusion.com/blogs/wp-content/uploads/2023/12/Install-Syncfusion.XlsIO_.Net_.Core-NuGet-package..png)

**Step 3** : Finally, add the following code to the **Program.cs** file to merge Excel documents from a designated folder path.

```csharp
using Syncfusion.XlsIO;

namespace MergeExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputPath = @"../../../Data/";

            string outputPath = @"../../../Output/";

            FileInfo[] files = new DirectoryInfo(inputPath).GetFiles();

            List<Stream> streams = new List<Stream>();

            foreach (FileInfo file in files)
            {
                streams.Add(file.OpenRead());
            }

            Stream mergedStream = MergeExcelDocuments(streams);

            FileStream fileStream = new FileStream(outputPath + "MergedExcel.xlsx", FileMode.Create, FileAccess.Write);
            mergedStream.Position = 0;
            mergedStream.CopyTo(fileStream);
            fileStream.Close();
        }

        /// <summary>
        /// Merge Excel documents from the list of Excel streams.
        /// </summary>
        /// <param name="streams">List of Excel document stream to be merged</param>
        /// <returns></returns>
        public static Stream MergeExcelDocuments(List<Stream> streams)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Xlsx;
                IWorkbook workbook = application.Workbooks.Create(0);

                //Loop through each Excel document and add the worksheets to the new workbook.
                foreach (Stream stream in streams)
                {
                    stream.Position = 0;
                    IWorkbook tempWorkbook = application.Workbooks.Open(stream);
                    workbook.Worksheets.AddCopy(tempWorkbook.Worksheets);
                    tempWorkbook.Close();
                }

                //Save the workbook to a memory stream.
                MemoryStream memoryStream = new MemoryStream();
                workbook.Version = ExcelVersion.Xlsx;
                workbook.SaveAs(memoryStream);

                return memoryStream;
            }
        }
    }
}
```

Refer to the following images showing the input Excel documents. ![Input Excel document](https://www.syncfusion.com/blogs/wp-content/uploads/2023/12/Input-Excel-document-1.png) ![Input Excel document](https://www.syncfusion.com/blogs/wp-content/uploads/2023/12/Input-Excel-document-2.png) ![Input Excel document](https://www.syncfusion.com/blogs/wp-content/uploads/2023/12/Input-Excel-document-3-1.png) <figcaption>Input Excel documents</figcaption>

After executing the previous code example, the output will look like the following image. See the number of Excel sheets added at the bottom of the Excel file.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/12/Merging-multiple-Excel-documents-with-the-Syncfusion-Excel-Library-using-csharp-768x614.png" alt="Merging multiple Excel documents with the Syncfusion Excel Library using C#" style="width:100%">

  <figcaption>Merging multiple Excel documents with the Syncfusion Excel Library using C#</figcaption>

</figure>

## GitHub reference

You can download the example for merging Excel documents in C# on [this GitHub page](https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/Real%20Time%20Examples%20in%20Excel/Merge%20Excel%20Documents/.NET%20Core "Merge Multiple Excel Files into One in Just 3 Steps Using C# GitHub demo").

## Wrapping up

Thanks for reading! As you can see, the [Syncfusion Excel (XlsIO) Library](https://www.syncfusion.com/excel-framework/net ".NET Excel Library") lets you effortlessly merge multiple Excel documents into a single document in C# with just three simple steps. Take a moment to peruse the Excel Library [documentation](https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-data-to-worksheets "Importing Data to Worksheets"), where you’ll find other importing options and features like [data tables](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-datatable "Import Data from DataTable"), [collection objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-collection-objects "Import Data from Collection Objects"), [grid view](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-dataview "Import Data from DataView"), [data columns](https://help.syncfusion.com/file-formats/xlsio/working-with-data#import-data-from-datacolumn "Importing Data from DataColumn"), and [HTML](https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-html-table-to-excel-worksheet "Importing HTML Table to Excel Worksheet"), all with accompanying code samples.

Using the Excel Library, you can export Excel data to [PDFs](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion "Excel to PDF Conversion"), [images](https://help.syncfusion.com/file-formats/xlsio/worksheet-to-image-conversion "Worksheet to Image conversion"), [data tables](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-data-table "Exporting from Worksheet to Data Table"), [CSV](https://www.syncfusion.com/kb/9092/convert-excel-file-to-csv-in-c-vb-net "Convert Excel file to CSV"), [TSV](https://www.syncfusion.com/kb/8556/does-xlsio-support-tsv-files "Excel to TSV conversion"), [HTML](https://help.syncfusion.com/file-formats/xlsio/working-with-excel-worksheet?_ga=2.19528508.561950334.1598519523-255771547.1598519523#save-worksheet-as-html "Save Worksheet as HTML"), [collections of objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-clr-objects "Working with Excel Data"), [ODS](https://help.syncfusion.com/file-formats/xlsio/excel-to-ods-conversion "Excel to ODS Conversion"), [JSON](https://help.syncfusion.com/file-formats/xlsio/excel-to-json-conversion "Excel to JSON Conversion"), and other file formats.

Are you already a Syncfusion user? You can download the product setup after [logging in](https://www.syncfusion.com/account/downloads/studio/ "Essential Studio License and Downloads page"). If you’re not yet a Syncfusion user, you can download a free 30-day trial [from this page](https://www.syncfusion.com/downloads/fileformats/confirm "Get free evaluation of Essential Studio products").

Please let us know in the comments section below if you have any questions about these features. You can also contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://www.syncfusion.com/support/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/excel "Syncfusion Feedback Portal"). We are always happy to assist you!

## Related blogs

- [6 Easy Ways to Export Data to Excel in C#](https://www.syncfusion.com/blogs/post/6-easy-ways-to-export-data-to-excel-in-c-sharp.aspx "Blog: 6 Easy Ways to Export Data to Excel in C#")
- [Seamlessly Import and Export CSV Data in Excel Using C#](https://www.syncfusion.com/blogs/post/import-export-csv-data-in-excel-csharp.aspx "Blog: Seamlessly Import and Export CSV Data in Excel Using C#")
- [How to Export Data from SQL Server to Excel Tables in C#](https://www.syncfusion.com/blogs/post/export-data-from-sql-server-to-excel-in-c-sharp.aspx "Blog: How to Export Data from SQL Server to Excel Tables in C#")
- [Export Data from Collections to Excel and Group It in C# ](https://www.syncfusion.com/blogs/post/export-data-from-collection-to-excel-and-group-csharp.aspx "Blog: Export Data from Collections to Excel and Group It in C#")
- [Export Data to a Predefined Excel Template in C#](https://www.syncfusion.com/blogs/post/export-data-to-a-predefined-excel-template-in-c.aspx "Blog: Export Data to a Predefined Excel Template in C#")
- [Easy Steps to Export HTML Tables to an Excel Worksheet in C#](https://www.syncfusion.com/blogs/post/easy-steps-to-export-html-tables-to-an-excel-worksheet-in-c.aspx "Blog: Easy Steps to Export HTML Tables to an Excel Worksheet in C#")