C#/VB.NET: Merge Excel Files into One

Sometimes, we may get annoyed when we have to open many Excel files simultaneously. Merging Excel files of the same type or category can help us avoid the trouble and save us much time. This article will demonstrate how to merge Excel files into One in C# and VB.NET using Spire.XLS for .NET library.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Merge Multiple Excel Workbooks into One in C# and VB.NET

The following are the steps to merge multiple Excel workbooks into one:

  • Create a string array from the Excel file paths.
  • Initialize a Workbook object to create a new Excel workbook, and clear the default worksheets in the workbook using Workbook.Worksheets.Clear() method.
  • Initialize another temporary Workbook object.
  • Loop through the string array, load the current workbook into the temporary Workbook object using Workbook.LoadFromFile() method.
  • loop through the worksheets in the current workbook, then copy each worksheet from the current workbook to the new workbook using Workbook.Worksheets.AddCopy() method.
  • Save the new workbook to file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace MergeExcelFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a string array from Excel file paths
            string[] inputFiles = new string[] { "April.xlsx", "May.xlsx", "June.xlsx" };

            //Initialize a new Workbook object
            Workbook newWorkbook = new Workbook();
            //Clear the default worksheets
            newWorkbook.Worksheets.Clear();

            //Initialize another temporary Workbook object
            Workbook tempWorkbook = new Workbook();

            //Loop through the string array
            foreach (string file in inputFiles)
            {
                //Load the current workbook
                tempWorkbook.LoadFromFile(file);
                //Loop through the worksheets in the current workbook
                foreach (Worksheet sheet in tempWorkbook.Worksheets)
                {
                    //Copy each worksheet from the current workbook to the new workbook
                    newWorkbook.Worksheets.AddCopy(sheet, WorksheetCopyType.CopyAll);
                }
            }

            //Save the new workbook to file
            newWorkbook.SaveToFile("MergeWorkbooks.xlsx", ExcelVersion.Version2013);
        }
    }
}

The input Excel workbooks:

C#/VB.NET: Merge Excel Files into One

The merged Excel workbook:

C#/VB.NET: Merge Excel Files into One

Merge Multiple Excel Worksheets into One in C# and VB.NET

We can merge multiple worksheets in the same or different workbooks into one. The following steps show how to merge two Excel worksheets in the same workbook into a single worksheet:

  • Initialize a Workbook object and load an Excel file using Workbook.LoadFromFile() method.
  • Get the two worksheets that need to be merged using Workbook.Worksheets[sheetIndex] property. Note the sheet index is zero-based.
  • Get the used range of the second worksheet using Worksheet.AllocatedRange property.
  • Specify the destination range in the first worksheet using Worksheet.Range[rowIndex, columnIndex] property. Note the row and column indexes are 1-based.
  • Copy the used range of the second worksheet to the destination range in the first worksheet using CellRange.Copy(destRange) method.
  • Remove the second worksheet using XlsWorksheet.Remove() method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace MergeExcelWorksheets
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Get the first worksheet
            Worksheet sheet1 = workbook.Worksheets[0];
            //Get the second worksheet
            Worksheet sheet2 = workbook.Worksheets[1];

            //Get the used range in the second worksheet
            CellRange sourceRange = sheet2.AllocatedRange;
            //Specify the destination range in the first worksheet
            CellRange destRange = sheet1.Range[sheet1.LastRow + 1, 1];

            //Copy the used range of the second worksheet to the destination range in the first worksheet
            sourceRange.Copy(destRange);

            //Remove the second worksheet
            sheet2.Remove();

            //Save the result file
            workbook.SaveToFile("MergeWorksheets.xlsx", ExcelVersion.Version2013);
        }
    }
}

The input Excel worksheets:

C#/VB.NET: Merge Excel Files into One

The merged Excel worksheets:

C#/VB.NET: Merge Excel Files into One

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.