使用 IRONXL 如何从 Excel 文件中获取单元格值在 C# 中 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 In this article, we will discuss how you can get a specific cell's value from an Excel file using IronXL. 1. IronXL IronXL is a powerful software library for .NET developers that provides a simple and efficient way to read, write, and manipulate Excel spreadsheets in their applications. It offers a range of features that make working with Excel files easy and efficient, including the ability to create, modify, and delete worksheets, read and write cell data, and even perform complex calculations using Excel formulas. With IronXL, developers can quickly and easily incorporate Excel functionality into their applications, saving time and streamlining their development process. Whether you're building a finance application or data analysis tool, or simply need to read and write Excel files in your application, IronXL provides a flexible and reliable solution. 2. Prerequisites If you want to use the IronXL library to extract the value of a specific cell of an Excel file, you must fulfill certain prerequisites, which include: Installing Visual Studio on your computer as it's necessary to create a C# project. Installing ASP.NET on your system. Installing the IronXL library on your system to export data using it. You can obtain it by downloading the IronXL NuGet package from the NuGet Package Manager in Visual Studio. 3. Creating a New Project in Visual Studio To use the IronXL library for Excel-related tasks, you must first create a .NET project in Visual Studio. While any version of Visual Studio can be used, it is advisable to opt for the most recent version. There are multiple project templates to choose from, including Windows Forms and ASP.NET, depending on your specific needs. This tutorial will use the Console Application project template to illustrate how to work with IronXL. Create a new project window After selecting the project type, provide a name for the project and choose its location along with the desired framework for the project, such as .NET Core 6. Project configuration Once the solution is created, the program.cs file will be opened, enabling you to enter code and construct/run the application. Project with code open With this new Visual Studio project now created, let's install IronXL. 4. Install IronXL The IronXL Library can be downloaded and installed using a few different methods. But these two approaches are the simplest ones. These are: Using NuGet packages in Visual Studio. Using the Visual Studio Command Line. 4.1 Using Visual Studio To install the IronXL library, navigate to the NuGet Package Manager in Visual Studio. Simply open the NuGet Package Manager and search for IronXL in the Browse tab. Once you have located IronXL in the search results, select it and proceed with the installation. Once the installation is complete, you can start using the IronXL library in your project. The below screenshot shows how to open the NuGet Package Manager in Visual Studio. NuGet Package Manager The following shows IronXL in search results: IronXL search result 4.2 Using the Visual Studio Command-Line Many developers prefer to install packages using a command line interface. To install IronXL using the command line, follow these steps: In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console. Enter the following line in the Package Manager Console tab: Install-Package IronXL.Excel Now the package will be downloaded and installed to the current project and will be ready to use. Installing via command line 5. Get Specific Cell Values from an Excel File using IronXL Retrieving the data stored in a specific cell of an Excel worksheet using IronXL is an uncomplicated process that requires only a few lines of code. With this software library, developers can easily access the desired Excel cell value within their program. The following code example will demonstrate how to use IronXL to obtain the value property with a cell address. using IronXL; using System; using System.Linq; class Program { static void Main() { // Load the Excel workbook WorkBook workBook = WorkBook.Load("sample.xlsx"); // Access the first worksheet WorkSheet workSheet = workBook.WorkSheets.First(); // Define a range var range = workSheet["B2:B2"]; // This specifies the cell range to read // Get the value stored in cell B2 foreach (var cell in range) { Console.WriteLine($"Value in B2: {cell.Value}"); } } } using IronXL; using System; using System.Linq; class Program { static void Main() { // Load the Excel workbook WorkBook workBook = WorkBook.Load("sample.xlsx"); // Access the first worksheet WorkSheet workSheet = workBook.WorkSheets.First(); // Define a range var range = workSheet["B2:B2"]; // This specifies the cell range to read // Get the value stored in cell B2 foreach (var cell in range) { Console.WriteLine($"Value in B2: {cell.Value}"); } } } Imports IronXL Imports System Imports System.Linq Friend Class Program Shared Sub Main() ' Load the Excel workbook Dim workBook As WorkBook = WorkBook.Load("sample.xlsx") ' Access the first worksheet Dim workSheet As WorkSheet = workBook.WorkSheets.First() ' Define a range Dim range = workSheet("B2:B2") ' This specifies the cell range to read ' Get the value stored in cell B2 For Each cell In range Console.WriteLine($"Value in B2: {cell.Value}") Next cell End Sub End Class $vbLabelText $csharpLabel The above code example gets a value from cell B2 which will be printed to the console. Output Console 5.1. Read Range of Values from an Excel Worksheet By making some modifications to the code example provided above, it is possible to obtain a range of cell values from an Excel worksheet. This involves changing the range parameter passed to the WorkSheet object. Specifically, the range parameter must be updated to reflect the range of cells that contain the desired data. using IronXL; using System; using System.Linq; class Program { static void Main() { // Load the Excel workbook WorkBook workBook = WorkBook.Load("sample.xlsx"); // Access the first worksheet WorkSheet workSheet = workBook.WorkSheets.First(); // Define a new range covering B2 to B3 var range = workSheet["B2:B3"]; // Get values stored in the defined range foreach (var cell in range) { Console.WriteLine($"Value in {cell.Address}: {cell.Value}"); } } } using IronXL; using System; using System.Linq; class Program { static void Main() { // Load the Excel workbook WorkBook workBook = WorkBook.Load("sample.xlsx"); // Access the first worksheet WorkSheet workSheet = workBook.WorkSheets.First(); // Define a new range covering B2 to B3 var range = workSheet["B2:B3"]; // Get values stored in the defined range foreach (var cell in range) { Console.WriteLine($"Value in {cell.Address}: {cell.Value}"); } } } Imports IronXL Imports System Imports System.Linq Friend Class Program Shared Sub Main() ' Load the Excel workbook Dim workBook As WorkBook = WorkBook.Load("sample.xlsx") ' Access the first worksheet Dim workSheet As WorkSheet = workBook.WorkSheets.First() ' Define a new range covering B2 to B3 Dim range = workSheet("B2:B3") ' Get values stored in the defined range For Each cell In range Console.WriteLine($"Value in {cell.Address}: {cell.Value}") Next cell End Sub End Class $vbLabelText $csharpLabel The cell range is changed from [B2:B2] to [B2:B3]; this will print two values instead of just one in the console. Output Console 2 6. Conclusion Retrieving the value of a specific cell in an Excel worksheet using C# is a common task for many applications that work with Excel data. IronXL is a powerful software library for .NET developers that provides a simple and efficient way to read, write, and manipulate Excel spreadsheets in their applications. With IronXL, developers can quickly and easily incorporate Excel functionality into their applications, saving time and streamlining their development process. By following the steps outlined in this article, developers can learn how to connect IronXL with their C# project, retrieve cell values programmatically, automate tasks that involve Excel data, and create more efficient and reliable applications. IronXL is a versatile and reliable solution for working with Excel files in C# Applications. Please visit those links to learn more about detailed operations on cell addresses or how to import Excel files. Users of IronPDF can also benefit from the Iron Suite, a collection of software development tools that includes IronPDF, IronOCR, IronXL, IronBarcode, and IronWebscraper. 常见问题解答 如何从 Excel 文件中检索特定单元格值? 您可以通过加载 Excel 工作簿、访问所需工作表,并使用 WorkSheet.GetCellValue 方法来获取特定单元格的值。 使用 C# 检索 Excel 单元格值的前提条件是什么? 要使用 C# 检索 Excel 单元格值,您需要通过 NuGet 包管理器安装 Visual Studio、ASP.NET 和 IronXL 库。 我可以使用 C# 从 Excel 工作表中读取一范围的单元格值吗? 可以,使用 IronXL,您可以通过指定范围并使用 WorkSheet.GetRange 来从指定的 Excel 工作表中检索值。 如何使用命令行安装 IronXL? IronXL 可以通过在 Visual Studio 包管理器控制台中执行 Install-Package IronXL.Excel 命令来安装。 使用 IronXL 进行 C# 中的 Excel 数据处理有什么好处? IronXL 允许无缝集成 Excel 功能到您的 C# 应用程序中,简化数据处理,提高生产力,消除对 Interop 的依赖。 如何创建一个用于处理 Excel 文件的 C# 新项目? 在 Visual Studio 中创建一个新的 C# 项目,通过选择项目模板如控制台应用程序,命名项目,选择其位置,并选择所需的框架。 是否可以在 C# 中不使用 Interop 来操作 Excel 电子表格? 可以,IronXL 允许您无需依赖 Interop 而直接在 C# 中操作 Excel 电子表格,提供了更简单和有效的方法。 如何使用 IronXL 提升我在 C# 应用程序中的 Excel 数据分析? IronXL 使您能够高效地在 C# 应用程序中检索、修改和分析 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读取器。 阅读更多 如何在 C# 中读取 CSV 文件如何在 C# 中将 DataTable 导...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多