使用 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 IronXL is a feature-rich API that provides various functionalities to work with Excel files in C#. This tutorial will explore how to fetch data from Excel sheets in C# using IronXL. To follow this tutorial, you need to have the following: Microsoft Visual Studio installed on your machine Basic knowledge of C# programming IronXL library installed in your project You can download IronXL from the official website or through the NuGet Package Manager in Visual Studio. Installing IronXL in Visual Studio To install IronXL on a C# project, please follow these steps: Open Microsoft Visual Studio. Create a new project or open an existing project. Right-click on the project in the Solution Explorer and select "Manage NuGet Packages." In the "NuGet Package Manager" window, select the "Browse" tab. In the search box, type "IronXL" and press Enter. Select "IronXL.Excel" and click the "Install" button. Click the "I Accept" button to accept the license agreement. Wait for the installation to complete. Installing the IronXL Library in Visual Studio Project Once these steps are taken, IronXL should be installed and ready to use in your C# project. Step 1: Import the Required Libraries Add the following sample code to import the IronXL library and other necessary libraries. using IronXL; using System; using System.Data; using System.Data.OleDb; using System.Windows.Forms; using IronXL; using System; using System.Data; using System.Data.OleDb; using System.Windows.Forms; Imports IronXL Imports System Imports System.Data Imports System.Data.OleDb Imports System.Windows.Forms $vbLabelText $csharpLabel Using the IronXL namespace provides all the necessary classes and functions to work with Excel files using the IronXL library. Step 2: Load the Excel File The first step is to load the file. IronXL provides a WorkBook class to load an Excel file. We can use the WorkBook class to open an existing Excel file or create a new workbook. To load an existing Excel file, use the following code: WorkBook workbook = WorkBook.Load("path/to/excel/file.xlsx"); WorkBook workbook = WorkBook.Load("path/to/excel/file.xlsx"); Dim workbook As WorkBook = WorkBook.Load("path/to/excel/file.xlsx") $vbLabelText $csharpLabel This code loads the specified Excel file into a WorkBook object. To create a new WorkBook, use the following sample code: WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) $vbLabelText $csharpLabel This code creates a new workbook in the specified file format (here, XLSX). IronXL can load and create Excel Spreadsheets in the .XLS and the .XLSX file formats Step 3: Retrieve the Excel Sheet After loading the Excel file, it is necessary to indicate the Excel sheet from which to fetch data. A WorkBook can contain multiple WorkSheet objects. Each WorkSheet needs to specify the name of the worksheet before accessing data. WorkSheet worksheet = workbook.WorkSheets["Sheet1"]; WorkSheet worksheet = workbook.WorkSheets["Sheet1"]; Dim worksheet As WorkSheet = workbook.WorkSheets("Sheet1") $vbLabelText $csharpLabel This code retrieves the first worksheet (with worksheet name Sheet1) from the workbook. If you want to retrieve a worksheet by index, you can use the following code: WorkSheet worksheet = workbook.WorkSheets[0]; WorkSheet worksheet = workbook.WorkSheets[0]; Dim worksheet As WorkSheet = workbook.WorkSheets(0) $vbLabelText $csharpLabel This code retrieves the first worksheet from the workbook, assuming that it exists. Step 4: Retrieve Data from Excel Sheet From the retrieved Excel sheet, accessing data becomes possible. IronXL provides various methods to retrieve data from an Excel sheet. Here are some of the most commonly used methods: Indexer operator []: Retrieves the value of a single cell. GetRow: Retrieves a single row of data. GetColumn: Retrieves a single column of data. GetRange: Retrieves a range of cells. To retrieve a single cell's value, use the following code: object value = worksheet["A1"].Value; object value = worksheet["A1"].Value; Dim value As Object = worksheet("A1").Value $vbLabelText $csharpLabel This code retrieves the value of cell A1 in the worksheet. To retrieve a single row of data, use the following code: var dataRow = worksheet.GetRow(1); var dataRow = worksheet.GetRow(1); Dim dataRow = worksheet.GetRow(1) $vbLabelText $csharpLabel This code retrieves the first row of data in the worksheet. To retrieve a single column of data, use the following code: var dataColumn = worksheet.GetColumn(1); var dataColumn = worksheet.GetColumn(1); Dim dataColumn = worksheet.GetColumn(1) $vbLabelText $csharpLabel This code retrieves the first column of data in the worksheet. To retrieve a range of cells, use the following code: var cellRange = worksheet.GetRange("A1:B2"); var cellRange = worksheet.GetRange("A1:B2"); Dim cellRange = worksheet.GetRange("A1:B2") $vbLabelText $csharpLabel This code retrieves the range of cells from A1 to B2 in the worksheet. Step 5: Extract Data from Excel Sheet To extract a single value from the retrieved data, it needs to be cast to the appropriate data type. The following code shows how to extract an integer value from cell A1: int value = worksheet["A1"].IntValue; int value = worksheet["A1"].IntValue; Dim value As Integer = worksheet("A1").IntValue $vbLabelText $csharpLabel The following code can be used to extract a row of data: var dataRow = worksheet.GetRow(1); var values = dataRow.Values; foreach (var value in values) { int intValue = (int)value; // Do something with the extracted value } var dataRow = worksheet.GetRow(1); var values = dataRow.Values; foreach (var value in values) { int intValue = (int)value; // Do something with the extracted value } Dim dataRow = worksheet.GetRow(1) Dim values = dataRow.Values For Each value In values Dim intValue As Integer = CInt(value) ' Do something with the extracted value Next value $vbLabelText $csharpLabel This code extracts the first row of data and iterates through the values in the row, casting each value to an integer. Next, the following code shows how to extract a column of data: var dataColumn = worksheet.GetColumn(1); var values = dataColumn.Values; foreach (var value in values) { string strValue = (string)value; // Do something with the extracted value } var dataColumn = worksheet.GetColumn(1); var values = dataColumn.Values; foreach (var value in values) { string strValue = (string)value; // Do something with the extracted value } Dim dataColumn = worksheet.GetColumn(1) Dim values = dataColumn.Values For Each value In values Dim strValue As String = CStr(value) ' Do something with the extracted value Next value $vbLabelText $csharpLabel This code extracts the first column of data and iterates through the values in the column, casting each value to a string. The next example demonstrates how to extract a range of cells: var cellRange = worksheet.GetRange("A1:B2"); var values = cellRange.Values; foreach (var row in values) { foreach (var value in row) { // Do something with the extracted value } } var cellRange = worksheet.GetRange("A1:B2"); var values = cellRange.Values; foreach (var row in values) { foreach (var value in row) { // Do something with the extracted value } } Dim cellRange = worksheet.GetRange("A1:B2") Dim values = cellRange.Values For Each row In values For Each value In row ' Do something with the extracted value Next value Next row $vbLabelText $csharpLabel This code extracts the range of cells from A1 to B2 and iterates through the values in each cell. Conclusion This tutorial explored how to fetch data from Excel sheets in C# using IronXL, as well as load an Excel file, retrieve the required worksheet, retrieve data from the worksheet, extract data, and import data to a database. IronXL provides a feature-rich API to work with Excel files, making it easy to integrate Excel data into C# applications. With IronXL, we can read and write Excel files and interact with their data programmatically. Additionally, we can use IronXL to automate Excel tasks, such as generating reports, creating charts, and formatting cells. More about IronXL IronXL is a user-friendly and convenient library for reading and writing to Excel & CSV files, with small and easily memorable functions. This tutorial only scratched the surface of what IronXL can do. IronXL provides a wide range of additional features, such as cell formatting, math functions, and conditional formatting to name a few. Notable Benefits of Using IronXL Dedicated support from .NET engineers, cross-platform supports, Azure, AWS, and Docker. Simple installation via Microsoft Visual Studio Free trial for development, with licenses starting at $799 Easy integration into C# or VB.NET projects 常见问题解答 如何在C#中将HTML转换为PDF? 你可以使用IronPDF的RenderHtmlAsPdf方法将HTML字符串转换为PDF。你还可以使用RenderHtmlFileAsPdf将HTML文件转换为PDF。 如何在C#中从Excel表中获取数据而不使用Interop? 您可以使用IronXL在C#中从Excel表中获取数据,通过WorkBook类加载Excel文件,并使用GetRow()、GetColumn()和GetRange()等方法访问数据。 在C#中使用IronXL操作Excel文件的前提条件是什么? 您需要Microsoft Visual Studio、C#编程的基本知识以及通过NuGet程序包管理器安装的IronXL库才能在C#中操作Excel文件。 如何在我的Visual Studio项目中安装IronXL? 要安装IronXL,打开Visual Studio,右键单击解决方案资源管理器中的项目,选择“管理NuGet程序包”,然后在“浏览”选项卡中搜索'IronXL'。单击“安装”将其添加到您的项目中。 如何使用IronXL加载Excel文件? 使用WorkBook类加载Excel文件。例如,WorkBook workbook = WorkBook.Load("path/to/excel/file.xlsx"); 如何使用IronXL从工作簿中检索特定工作表? 您可以通过名称检索工作表:WorkSheet worksheet = workbook.WorkSheets["Sheet1"];或通过索引:WorkSheet worksheet = workbook.WorkSheets[0]; 如何使用IronXL从单元格中提取数据? 使用单元格的索引检索其值,如object value = worksheet["A1"].Value;并将其转换为适当的数据类型。 IronXL能否处理.XLS和.XLSX文件格式? 是的,IronXL可以加载和创建.XLS和.XLSX格式的Excel电子表格。 如何使用IronXL提取单元格范围? 要提取单元格范围,使用var cellRange = worksheet.GetRange("A1:B2");并遍历这些值。 使用IronXL有哪些优势? IronXL提供.NET工程师的专门支持、跨平台支持,通过Visual Studio简单安装、免费试用以及与C#或VB.NET项目的轻松集成。 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读取器。 阅读更多 如何在 Excel 中将表格转换为范围如何在 C# 中创建 CSV 文件...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多