使用 IRONXL 如何在 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 This article demonstrates how to merge multiple cells into one using Microsoft Excel and IronXL programmatically. Merge Multiple Cells into a Single Cell Using Microsoft Excel The built-in Merge and Center option in Excel is the quickest and simplest approach to integrate two or more cells. There are only two fast steps in the entire process: Choose the adjacent Excel cells that you want to combine into one cell. Click the Merge & Center button in the Home tab's Alignment group to merge two cells or multiple columns in Excel. In this example, the upper left cell A1 contains a list of fruits, and data in two adjacent empty cells (B1 and C1) is combined to create a single, sizable cell that can hold the complete list. Merging Cells The text is centered, and the selected cells are combined into one large cell once you click Merge & Center, as shown in the above screenshot. Excel's Options to Combine Multiple Cells Select the choice you want from the drop-down menu by clicking the tiny drop-down arrow next to the Merge & Center button, which will give you access to a few more merging options supplied by Excel, such as Merge Across, Unmerge Cells. Combine Cells Using the Merge Across command, each row's individually chosen cells are combined. Using the Merge Cells command, you can combine the selected cells into one without centering the text or losing data. Simply pick the merged cell and click the desired alignment in the Alignment group on the Home page to change the text alignment of only the data after merging all the cells. There are a few things to remember when using Excel's built-in functions to combine adjacent cells: Only the content of the upper-left cell will remain after merging, as all data in the other cells will be removed. Therefore, make sure that all the data you want to include in a merged cell is entered in the leftmost cell of the chosen range. The selected cells are likely in Edit mode if the Merge and Center button is grayed out. Try merging cells after exiting Edit mode by pressing the Enter key. IronXL Library Features You can quickly read and change Microsoft Excel documents with the IronXL library in C#. Without installing Microsoft Excel or relying on Microsoft Office Interop Excel, IronXL is a standalone .NET software library that can read other spreadsheet formats. One of the best Excel spreadsheet libraries for C# is IronXL, which works with both .NET Framework and .NET Core. It supports many versions of .NET Frameworks, including Console Applications, Windows Forms, and Web Applications. IronXL makes it simple and quick to read Excel files with or without merged cells. Numerous Excel file types are supported, including XLSX, XLS, CSV, TSV, XLST, XLSM, and others. Numerous procedures, including import, edit, export of data tables, export of datasets, and others, are available. With IronXL, you can export and save files in a variety of extensions, including XLS, CSV, TSV, JSON, and more. You can easily read, alter, and create Excel spreadsheet files in the .NET environment with IronXL's user-friendly C# API. It offers full support for Azure, .NET Core, .NET Framework, Xamarin, Mobile, Linux, and macOS. IronXL supports various Excel column data formats, including text, integers, formulas, dates, currencies, and percentages, and it is capable of carrying out calculations like Excel. Creating a New Project in Visual Studio Open Visual Studio, select "New project" and choose "Console App" from the File menu. A C# Console Application is used for simplicity. New Project In the relevant text box, type the project name and file path. Next, select the necessary .NET Framework by clicking the Create button. The project will now create the program.cs file's structure and open, allowing you to enter the program's code and build or run it. Project Configuration The solution's necessary IronXL library must then be downloaded. By entering the following command in the package manager, you can download the package: Install-Package IronXL.Excel IronXL The "IronXL" package can also be found and downloaded using the NuGet Package Manager. Dependency management in your project is made simple with the NuGet Package Manager. NuGet Package Manager Merge Cells Using IronXL IronXL can merge multiple columns/cells in existing Excel sheets. Below is the sample code to merge multiple cells. using IronXL; class Program { static void Main() { // Load the existing Excel workbook var excelDoc = WorkBook.Load("demo.xlsx"); // Get the default worksheet WorkSheet workSheet = excelDoc.DefaultWorkSheet; // Define the range of cells to merge var range = workSheet["A1:C1"]; // Merge the specified range of cells workSheet.Merge(range.RangeAddressAsString); // Save the changes made to the workbook excelDoc.Save(); } } using IronXL; class Program { static void Main() { // Load the existing Excel workbook var excelDoc = WorkBook.Load("demo.xlsx"); // Get the default worksheet WorkSheet workSheet = excelDoc.DefaultWorkSheet; // Define the range of cells to merge var range = workSheet["A1:C1"]; // Merge the specified range of cells workSheet.Merge(range.RangeAddressAsString); // Save the changes made to the workbook excelDoc.Save(); } } Imports IronXL Friend Class Program Shared Sub Main() ' Load the existing Excel workbook Dim excelDoc = WorkBook.Load("demo.xlsx") ' Get the default worksheet Dim workSheet As WorkSheet = excelDoc.DefaultWorkSheet ' Define the range of cells to merge Dim range = workSheet("A1:C1") ' Merge the specified range of cells workSheet.Merge(range.RangeAddressAsString) ' Save the changes made to the workbook excelDoc.Save() End Sub End Class $vbLabelText $csharpLabel In the example code above: The existing Excel workbook is loaded into the IronXL workbook object. The default worksheet is selected. The range of cells (A1:C1) that you want to merge is specified. The Merge method is then called to combine the specified range of cells. Finally, the Save method is called to save the changes to the workbook. This Merge function merges a group of cells, retaining only the value of the first cell in the range without removing the values from other cells. However, the values from those merged cells are still accessible in IronXL. Conclusion A highly popular Excel add-in, IronXL, doesn't rely on external libraries. It is a standalone solution that doesn't require Microsoft Excel to be installed. It works with a variety of platforms. With IronXL, you may programmatically perform a vast array of functions on Microsoft Excel documents. You can sort strings or numbers, trim and add data in empty cells, look up and replace values in blank cells, merge and unmerge cells, save files, concatenate functions, and perform other operations. You can specify cell data types and evaluate spreadsheet data with it as well. IronXL also has CSV file reading and writing capabilities. When IronXL is released, it will cost $799 to acquire. Additionally, customers have the option to pay an annual membership fee for product upgrades and support. IronXL offers unrestricted redistribution rights for an extra fee. You can click on the licensing page here to go to the appropriate source and get more specific pricing details. 常见问题解答 如何在 Excel 中合并单元格而不丢失数据? 您可以使用 IronXL 来合并 Excel 单元格而不丢失数据。IronXL 提供了一种编程解决方案,可以保留所有合并单元格中的数据,而不像原生 Excel 功能那样只保留左上角单元格的内容。 使用 C# 合并 Excel 单元格的编程方法是什么? 使用 IronXL,您可以通过加载工作簿、选择工作表和范围、应用 Merge 方法来合并 Excel 单元格,然后保存工作簿。此过程确保所有数据被保留。 IronXL 在合并单元格时如何处理数据,与 Excel 的内置功能相比有什么不同? IronXL 保留正在合并的所有单元格的数据,确保不会丢失任何信息。相比之下,Excel 的内置“合并及居中”功能仅保留左上角单元格的数据。 我可以使用 IronXL 在不同平台上的 Excel 中合并单元格吗? 可以,IronXL 支持多个平台,包括 .NET Framework 和 .NET Core,允许您在控制台应用程序、Windows 窗体和 Web 应用程序等多种环境中合并 Excel 文件中的单元格。 是否需要安装 Microsoft Excel 才能使用 IronXL 进行 Excel 操作? 不需要,IronXL 是一个独立的 .NET 库,无需在您的系统上安装 Microsoft Excel。它允许您独立地执行 Excel 操作。 使用 IronXL 合并 Excel 单元格的步骤是什么? 要使用 IronXL 合并单元格,首先将 Excel 文件加载到您的应用程序中。选择工作表和要合并的单元格范围。在选择的范围上使用 IronXL 的 Merge 方法,然后将更改保存到文件中。 如何在一个 Visual Studio 项目中安装 IronXL 以进行 Excel 操作? 您可以通过 NuGet 包管理器在一个 Visual Studio 项目中安装 IronXL。搜索“IronXL”并进行安装,或者在包管理器控制台中使用命令 Install-Package IronXL。 IronXL 是否可以用于执行除合并单元格之外的其他 Excel 操作? 可以,IronXL 支持一系列的 Excel 操作,除了合并单元格之外,还包括读取、编辑和创建 Excel 文件,数据导入/导出,以及执行类似于 Excel 的计算。 使用 IronXL 有哪些许可选项? IronXL 提供许可选项,包括年费会员制,包含产品升级和支持。详细的定价信息可在他们的许可页面上找到。 IronXL 支持哪些 Excel 格式? IronXL 支持多种 Excel 格式,如 XLSX、XLS 和 CSV,允许对不同 Excel 文件类型进行广泛的文档操作和兼容性处理。 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读取器。 阅读更多 如何在 ASP .NET Core 中打开 Excel 文件如何在 C# 中将 XLS 转换为 X...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多