Logo

dev-resources.site

for different kinds of informations.

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

Published at
12/8/2023
Categories
fileformats
csharp
excel
productivity
Author
jollenmoyani
Author
12 person written this
jollenmoyani
open
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.

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;
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Refer to the following images showing the input Excel documents. Input Excel document Input 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 C#

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.

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 tables, collection objects, grid view, data columns, and HTML, all with accompanying code samples.

Using the Excel Library, you can export Excel data to PDFs, images, data tables, CSV, TSV, HTML, collections of objects, ODS, JSON, 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 forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

fileformats Article's
30 articles in total
Favicon
Introducing the Syncfusion® Document Viewer Extension for Visual Studio Code
Favicon
What’s New in Document Processing Libraries: 2024 Volume 4
Favicon
How to Effectively Use Formulas in Excel Using C#
Favicon
Easily Create Dynamic Charts in Excel Using C#
Favicon
6 Effective Ways to Merge PDF Files Using C#
Favicon
What’s New in Document Processing Libraries: 2024 Volume 3
Favicon
3 Easy Steps to Add Watermarks to Your Excel Document Using C#
Favicon
Easily Create PDF Tables with Advanced Customization in C#
Favicon
Easily Convert Your PDF to Images in C#
Favicon
4 Simple Ways to Split Word Documents Using C#
Favicon
What’s New in 2024 Volume 2: Document Processing Libraries
Favicon
Create Excel Table in Just 3 Steps Using C#
Favicon
Easily Create an Excel Pivot Table in Just 3 Steps Using C#
Favicon
What’s New in 2024 Volume 1: Document Processing Libraries
Favicon
What’s New in 2023 Volume 4: File Format Libraries
Favicon
Converting XLS to XLSX Format in Just 3 Steps Using C#
Favicon
Print Excel Documents in Just 4 Steps Using C#
Favicon
3 Simple Steps to Split an Excel File into Multiple Excel Files in C#
Favicon
An Ultimate Solution to Convert EML to PST Format
Favicon
Easily Convert Organizational Chart Diagrams to PowerPoint Presentations
Favicon
Merge Multiple Excel Files into One in Just 3 Steps Using C#
Favicon
Automate Email Distribution of Income Tax Reports from Excel using C#
Favicon
Top 10 Must-Have Features in C# PDF Library
Favicon
How to Convert OST to MBOX Format: 2 Effective Techniques
Favicon
Optical Character Recognition (OCR) Made Easy with the .NET PDF Library in C#
Favicon
Secure Your PDF Documents Like a Pro Using C#
Favicon
Effortlessly Compare Word Documents Using C#
Favicon
Explore the Possibilities of the What-If Analysis Scenario Manager in Excel Using C#
Favicon
How to Add Comments to Excel Documents Using C#
Favicon
Convert Word Documents to Markdown and Vice Versa in C#

Featured ones: