Merge Multiple Excel Files into One in Just 3 Steps Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Merge Multiple Excel Files into One in Just 3 Steps Using C#

Merge Multiple Excel Files into One in Just 3 Steps Using C#

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 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.

Enjoy a smooth experience with Syncfusion’s Excel Library! Get started with a few lines of code and without Microsoft or interop dependencies.

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 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. Then, create a new .NET Core console application.Create a .NET Core Console application

Step 2: Download and install the most recent version of the Syncfusion.XlsIO.Net.Core NuGet package.Install Syncfusion.XlsIO.Net.Core NuGet package

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

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 documentInput Excel document Input Excel document

Input Excel documents

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.

Merging multiple Excel documents with the Syncfusion Excel Library using csharp
Merging multiple Excel documents with the Syncfusion Excel Library using C#

GitHub reference

You can download the example for merging Excel documents in C# on this GitHub page.

Don't settle for ordinary spreadsheet solutions. Switch to Syncfusion and upgrade the way you handle Excel files in your apps!

Wrapping up

Thanks for reading! As you can see, the Syncfusion Excel (XlsIO) 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, where you’ll find other importing options and features like data tablescollection objects, grid viewdata columns, and HTML, all with accompanying code samples.

Using the Excel Library, you can export Excel data to PDFs, images, data tables, CSVTSVHTMLcollections of objectsODSJSON, and other file formats.

Are you already a Syncfusion user? You can download the product setup after logging in. If you’re not yet a Syncfusion user, you can download a free 30-day trial from this page.

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 forumsupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed