In this article, you are going to learn how to copy sheets from multiple Excel workbooks into one workbook using C#. You will also learn how to copy data from several worksheets into one sheet. Let’s start.

C# API to Merge Multiple Excel files

Aspose.Cells for .NET is a well-known spreadsheet manipulation API that lets you create and process Excel files from within your .NET applications. You can either download the binaries of the API or get it installed using NuGet.

PM> Install-Package Aspose.Cells

Combine Multiple Excel Files into One using C#

Sometimes, you need to merge multiple Excel files into one file. You want to copy worksheets from the source workbooks to the destination workbook as shown below. Excel documents can be of any version such as Excel 97, Excel 2010 or Excel 2016.

Combine Multiple Excel Files into One using C#

Fig 1: Combine Excel Files

The following sample code shows how to combine multiple Excel files into one using C#.

Combine Specific Worksheets of Excel Files using C#

The above code copies all worksheets in the source files to the destination file. However, you may want to copy specific worksheets from the source files to the destination file. For example, you have two Excel files and each has three worksheets named Sales, Employees and Expenses. You only want to copy Sales worksheet from both files to the destination file as shown in the following figure.

Combine Specific Worksheets of Excel Files using C#

Fig 2: Combine Specific Worksheets of Excel Files

The following sample code shows how to combine specific worksheets of source files into destination file using C#.

Merge Multiple Worksheets into One using C#

Sometimes, you need to copy data from several worksheets into one worksheet. For example, you have a few worksheets in an Excel file that contain information about different products, and you want to merge these sheets into one summary worksheet, like this:

Merge Multiple Worksheets into One using C#

Fig 3: Merge Multiple Worksheets into One

The following code snippet shows how to copy data from several worksheets into one worksheet using C#.

Conclusion

In this article, you have learnt how to programmatically combine multiple Excel files into one. You can either copy all worksheets of a source file or specific ones to the destination file. You have also learnt how to combine data of multiple worksheets into one worksheet. Please check the documentation of Aspose.Cells for .NET for more information. If you have any questions, please feel free to ask at our Support Forum. We will answer them in a few hours.

See Also