使用 IRONXL 如何在 C# 中连接或合并 Excel 文件 Curtis Chau 已更新:六月 22, 2025 Download IronXL NuGet 下载 DLL 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Microsoft created Microsoft Excel, a powerful spreadsheet program. It is a commonly used tool for organizing, analyzing, and visualizing data. It is a component of the Microsoft Office suite. Excel is a flexible application that may be used by people, professionals, academics, and corporations due to its many capabilities. Many industries, including finance, accounting, business analysis, data analysis, research, education, and more, utilize Excel extensively. It is a preferred tool for organizing, evaluating, and displaying data in both personal and professional contexts due to its broad feature set and adaptability. IronXL is a powerful Excel library that can be used to perform various types of Excel operations. In this article, we are going learn how to concatenate or merge Excel files in C#. How to merge Excel files in C# Create a new project in Visual Studio Install IronXL library into the project Load all the Excel files that need to be merged Extract all the sheets from the loaded Excel sheet Add the Sheets to a new Excel file Save them into a separate file IronXL Library IronXL is a substitute for Microsoft Interop when it comes to managing Excel files in .NET applications. While IronXL offers a more straightforward, effective, and powerful method of manipulating Excel files programmatically in .NET settings, Microsoft Interop requires the use of the Interop components to communicate with Excel. The use of IronXL has the following benefits: Performance and Resource Economy: IronXL performs better because it does not depend on the Excel application being installed on the PC. Readability and Simplicity: IronXL provides a more straightforward API, making it easier to read, write, and manipulate Excel files without the complexities of Microsoft Interop. Compatibility and Dependency: Since IronXL does not require Microsoft Excel to be installed, it eliminates dependencies and compatibility issues with various Office or Excel versions. Platform Independence: IronXL offers greater flexibility and ease of deployment across different environments and platforms, whereas Microsoft Interop is more closely tied to specific Microsoft Office versions. For .NET developers who need to work with Excel files programmatically, IronXL is often a better choice due to its performance, user-friendliness, and reduced reliance on additional applications. The decision between IronXL and Microsoft Interop may be influenced by factors such as the project's specific requirements, existing infrastructure, and the developer's familiarity with each library. Always consider your application's needs in making this choice. Visit this page to learn more about the IronXL library. Creating a New Project in Visual Studio To launch the Visual Studio application, click "File" from the menu, then "New Project," and choose "Console application." Enter the project name and the file location. Click the "Create" button and choose the required .NET Framework as shown below. The project's structure will depend on the selected application type. Use the console, Windows, or web application to build or execute the application and add code by entering the Program.cs file. Then the library can be added, and the code tested. Installing IronXL Library To install the IronXL library, open the NuGet Package Manager Console and type the following command: Install-Package IronXL.Excel Alternatively, use the NuGet Package Manager to search for "IronXL" and download it from the list of related packages. Combine Excel files using IronXL With IronXL, we can combine multiple Excel files or worksheets into a single Excel file or worksheet using the following code: using IronXL; using System; class Program { static void Main(string[] args) { // Array of file paths for the Excel files to be merged string[] filePaths = { "file1.xlsx", "file2.xlsx" }; // Create a new workbook to add sheets from the existing files WorkBook newWorkBook = WorkBook.Create(); try { foreach (var filePath in filePaths) { // Load the existing Excel file into a workbook WorkBook existingWorkbook = WorkBook.LoadExcel(filePath); // Retrieve the sheets from the loaded workbook WorksheetsCollection sheetCollection = existingWorkbook.WorkSheets; // Add each sheet from the existing workbook to the new workbook foreach (var sheet in sheetCollection) { newWorkBook.WorkSheets.Add(sheet); } } // Save the new workbook with a merged sheet collection newWorkBook.SaveAs("MergedBook.xls"); } catch (Exception ex) { // Output any exceptions encountered during the merging process Console.WriteLine(ex.ToString()); } } } using IronXL; using System; class Program { static void Main(string[] args) { // Array of file paths for the Excel files to be merged string[] filePaths = { "file1.xlsx", "file2.xlsx" }; // Create a new workbook to add sheets from the existing files WorkBook newWorkBook = WorkBook.Create(); try { foreach (var filePath in filePaths) { // Load the existing Excel file into a workbook WorkBook existingWorkbook = WorkBook.LoadExcel(filePath); // Retrieve the sheets from the loaded workbook WorksheetsCollection sheetCollection = existingWorkbook.WorkSheets; // Add each sheet from the existing workbook to the new workbook foreach (var sheet in sheetCollection) { newWorkBook.WorkSheets.Add(sheet); } } // Save the new workbook with a merged sheet collection newWorkBook.SaveAs("MergedBook.xls"); } catch (Exception ex) { // Output any exceptions encountered during the merging process Console.WriteLine(ex.ToString()); } } } Imports IronXL Imports System Friend Class Program Shared Sub Main(ByVal args() As String) ' Array of file paths for the Excel files to be merged Dim filePaths() As String = { "file1.xlsx", "file2.xlsx" } ' Create a new workbook to add sheets from the existing files Dim newWorkBook As WorkBook = WorkBook.Create() Try For Each filePath In filePaths ' Load the existing Excel file into a workbook Dim existingWorkbook As WorkBook = WorkBook.LoadExcel(filePath) ' Retrieve the sheets from the loaded workbook Dim sheetCollection As WorksheetsCollection = existingWorkbook.WorkSheets ' Add each sheet from the existing workbook to the new workbook For Each sheet In sheetCollection newWorkBook.WorkSheets.Add(sheet) Next sheet Next filePath ' Save the new workbook with a merged sheet collection newWorkBook.SaveAs("MergedBook.xls") Catch ex As Exception ' Output any exceptions encountered during the merging process Console.WriteLine(ex.ToString()) End Try End Sub End Class $vbLabelText $csharpLabel Explanation: File Paths: An array is used to store the file paths for all Excel files intended to be merged. Workbooks Creation: A new Excel workbook is created with WorkBook.Create() to store all combined sheets. File Loading: Iterates over the file paths, loading each Excel file into a WorkBook object using WorkBook.LoadExcel(filePath). Sheet Collection: Retrieves all the worksheets from each loaded workbook. Sheet Addition: Adds each worksheet from the existing files to the new workbook using the Add method. File Saving: The new workbook is saved as "MergedBook.xls", thus completing the merge process. Below is the sample input file used for merging Excel files. Resulting merged file: For more information about IronXL, visit this page. Conclusion IronXL is a popular add-on for Excel as it operates independently of any other external libraries. Since Microsoft Excel is self-contained, there is no need to have it installed separately, and tasks can be performed without additional dependencies. This contrasts with the Interop library, which requires other libraries to parse files for Word documents. IronXL offers new ways to handle Microsoft Excel documents in programming. It allows operations like calculations, sorting, trimming, finding and replacing, and file storing, among others. It enables reading, writing, and managing Excel data efficiently. The initial cost of IronXL is a $799. Alternatively, customers can opt for a one-year membership fee to receive support and software upgrades. A paid plan also offers protection against unauthorized redistribution. For a free trial of IronXL, click this link. For detailed pricing information, visit the IronXL licensing website. For further details on Iron Software products, visit this page. 常见问题解答 如何使用 C# 将多个 Excel 文件合并为一个文件? 您可以使用 IronXL 将多个 Excel 文件合并为一个,方法是加载每个 Excel 文件,提取工作表,并将它们添加到一个新的工作簿中。在合并了来自不同文件的所有工作表后,将新工作簿保存为一个合并的 Excel 文件。 IronXL 比 Microsoft Interop 在合并 Excel 文件方面的优势是什么? IronXL 比 Microsoft Interop 更好,因为它可以独立于 Microsoft Excel 安装运行,提供了改进的性能、跨平台的简便部署,以及一个用于 Excel 文件操作的简单 API。 如何在 Visual Studio 项目中安装 IronXL? 要在 Visual Studio 项目中安装 IronXL,请使用命令 Install-Package IronXL 的 NuGet 包管理器控制台,或者在 NuGet 包管理器中搜索 'IronXL' 并从列表中下载。 是否可以在没有安装 Microsoft Excel 的情况下在 C# 中操作 Excel 文件? 是的,使用 IronXL,您可以在没有安装 Microsoft Excel 的情况下在 C# 中操作 Excel 文件,因为 IronXL 的操作不依赖于 Excel。 使用 IronXL 合并 Excel 文件的主要步骤是什么? 使用 IronXL 合并 Excel 文件的主要步骤包括创建一个新的工作簿,加载现有的 Excel 文件,从这些文件中提取工作表,将工作表添加到新的工作簿,并保存合并后的工作簿。 如何在 Visual Studio 中开始一个用于 Excel 文件操作的项目? 要开始一个用于 Excel 文件操作的 Visual Studio 项目,创建一个新的“控制台应用程序”项目,选择“文件”,然后选择“新建项目”,并根据您的需求选择合适的 .NET Framework。 IronXL 与传统的 Excel 操作方法相比有什么好处? IronXL 提供的好处包括不需要安装 Excel、性能改进、资源经济、简单和直观的 API 以及平台独立性,使其成为现代 .NET 应用程序的理想选择。 IronXL 如何处理来自不同版本的 Excel 文件的合并? IronXL 无缝处理来自不同版本的 Excel 文件的合并,因为它设计为能够独立于特定的 Excel 或 Office 版本工作,从而确保兼容性和易用性。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已发布十月 27, 2025 如何在 C# 中创建 Excel 数据透视表 学习通过这个清晰的分步指南使用C# Interop和IronXL在Excel中创建数据透视表。 阅读更多 已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多 已发布十月 27, 2025 如何在.NET Core中使用CSV Reader与IronXL 学习通过实际示例有效地使用IronXL作为.NET Core的CSV读取器。 阅读更多 如何在 C# 中无需 oledb 将 Excel 转换为 DataTable如何在 C# 中加载 Excel 文件
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多