IronXL 操作指南 C Sharp 读取 XLSX 文件 C# Read XLSX File Curtis Chau 已更新:八月 20, 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 was translated from English: Does it need improvement? Translated View the article in English When working with various Excel formats, it often requires reading the data and manipulating it programmatically. In the following tutorial, we will learn how to read data from an Excel spreadsheet in C# using a convenient tool, IronXL. Quickstart: Load a workbook and access a worksheet effortlessly With IronXL, you can load an XLSX file using the WorkBook.Load method in a single line. Then access its first or named worksheet instantly and begin reading cell values — fast, frictionless Excel file reading. Get started making PDFs with NuGet now: Install IronXL with NuGet Package Manager PM > Install-Package IronXL.Excel Copy and run this code snippet. IronXL.WorkBook workbook = IronXL.WorkBook.Load("your-file.xlsx"); Deploy to test on your live environment Start using IronXL in your project today with a free trial Free 30 day Trial Read .XLSX Files C# Get IronXL for your project Load a WorkBook Access data from a WorkSheet Apply functions like Sum, Min, & Max Read a WorkSheet as a DataTable, DataSet, and more 1. Get IronXL for Your Project Use IronXL in your project for a simple way to work with Excel file formats in C#. You can either install IronXL via direct download or alternatively you can use NuGet Install for Visual Studio. The software is free for development. Install-Package IronXL.Excel How to Tutorial 2. Load WorkBook WorkBook is the class of IronXL whose object provides full access to the Excel file and its whole functions. For example, if we want to access an Excel file, we'd use the code: :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-load-workbook.cs using IronXL; // Load the workbook WorkBook workBook = WorkBook.Load("sample.xlsx"); // Excel file path IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel In the above code, the WorkBook.Load() function loads sample.xlsx into workBook. Any type of function can be performed on workBook by accessing the specific WorkSheet of an Excel file. 3. Access Specific WorkSheet To access a specific WorkSheet of an Excel file, IronXL provides the WorkSheet class. It can be used in a few different ways: :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-get-worksheet.cs using IronXL; // Access sheet by name WorkSheet workSheet = workBook.GetWorkSheet("Sheet1"); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel workBook is the WorkBook that is declared in the above portion. OR :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-worksheet-index.cs using IronXL; // Access sheet by index WorkSheet workSheet = workBook.WorkSheets[0]; IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel OR :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-default-worksheet.cs using IronXL; // Access the default worksheet WorkSheet workSheet = workBook.DefaultWorkSheet; IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel OR :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-first-worksheet.cs using IronXL; using System.Linq; // Access the first worksheet WorkSheet workSheet = workBook.WorkSheets.First(); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel OR :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-first-or-default-worksheet.cs using IronXL; using System.Linq; // Access the first or default worksheet WorkSheet workSheet = workBook.WorkSheets.FirstOrDefault(); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel After getting ExcelSheet workSheet, you can get any type of data from it and perform all Excel functions on it. 4. Access Data from WorkSheet Data can be accessed from ExcelSheet workSheet with this process: :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-get-data.cs using IronXL; // Accessing data as a string string dataString = workSheet["A1"].ToString(); // Accessing data as an integer int dataInt = workSheet["B1"].Int32Value; IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel It is also possible to get data from multiple cells of a specific column. foreach (var cell in workSheet["A2:A10"]) { Console.WriteLine("Value is: {0}", cell.Text); } foreach (var cell in workSheet["A2:A10"]) { Console.WriteLine("Value is: {0}", cell.Text); } For Each cell In workSheet("A2:A10") Console.WriteLine("Value is: {0}", cell.Text) Next cell $vbLabelText $csharpLabel This will display the values from cell A2 to A10. A complete code example of the specifics above is provided here. :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-log-data.cs using IronXL; using System; // Load an Excel file WorkBook workBook = WorkBook.Load("sample.xlsx"); WorkSheet workSheet = workBook.GetWorkSheet("Sheet1"); // Specify the range foreach (var cell in workSheet["B2:B10"]) { Console.WriteLine("Value is: {0}", cell.Text); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel It will display the following result: With the Excel file Sample.xlsx : We can see how effortless it is to use Excel file data in your project using these methodologies. 5. Perform Functions on Data It is very straightforward to access filtered data from an Excel WorkSheet by applying aggregate functions like Sum, Min, or Max using the following code: :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-aggregate-function.cs using IronXL; // Apply aggregate functions decimal sum = workSheet["G2:G10"].Sum(); // Sum of cells from G2 to G10 decimal min = workSheet["G2:G10"].Min(); // Minimum value in cells from G2 to G10 decimal max = workSheet["G2:G10"].Max(); // Maximum value in cells from G2 to G10 IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel If you want more details, check out our in-depth tutorial on How to Write C# Excel Files with specifics on aggregate functions. :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-min-max.cs using IronXL; using System; // Load the Excel workbook WorkBook workBook = WorkBook.Load("sample.xlsx"); // Get the specified WorkSheet WorkSheet workSheet = workBook.GetWorkSheet("Sheet1"); // Calculate sum, minimum, and maximum for a range of cells decimal sum = workSheet["G2:G10"].Sum(); decimal min = workSheet["G2:G10"].Min(); decimal max = workSheet["G2:G10"].Max(); // Output results Console.WriteLine("Sum is: {0}", sum); Console.WriteLine("Min is: {0}", min); Console.WriteLine("Max is: {0}", max); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel This code will display the following output: And this is how the Excel file Sample.xlsx will look: 6. Read Excel WorkSheet as DataTable Using IronXL, it is very easy to operate with an Excel WorkSheet as a DataTable. :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-datatable.cs using IronXL; using System.Data; // Convert worksheet to DataTable DataTable dt = workSheet.ToDataTable(); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel If we want to use the first row of ExcelSheet as DataTable ColumnName then: :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-datatable-header.cs using IronXL; using System.Data; // Convert worksheet to DataTable with the first row as column names DataTable dt = workSheet.ToDataTable(true); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel Thus, the Boolean parameter of ToDataTable() sets the first row as the column names of your DataTable. By default, its value is False. :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-print-datatable.cs using IronXL; using System; using System.Data; // Load the Excel workbook WorkBook workBook = WorkBook.Load("sample.xlsx"); // Get the specified WorkSheet WorkSheet workSheet = workBook.GetWorkSheet("Sheet1"); // Convert WorkSheet to DataTable DataTable dt = workSheet.ToDataTable(true); // Use first row as column names // Iterate through rows and columns and display data foreach (DataRow row in dt.Rows) // Access rows { for (int i = 0; i < dt.Columns.Count; i++) // Access columns of corresponding row { Console.Write(row[i] + " "); } Console.WriteLine(); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel Using the above code, every cell value of the WorkSheet can be accessed and used as required. 7. Read Excel File as DataSet IronXL provides a very simple function to use a complete Excel file (WorkBook) as a DataSet. Use the ToDataSet method to turn the whole workbook into DataSet. In this example, we will see how to use the Excel file as a DataSet. :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-excel-to-dataset.cs using IronXL; using System; using System.Data; // Load the Excel workbook WorkBook workBook = WorkBook.Load("sample.xlsx"); // Convert the WorkBook to a DataSet DataSet ds = workBook.ToDataSet(); // Iterate through tables in the DataSet and display table names foreach (DataTable dt in ds.Tables) { Console.WriteLine(dt.TableName); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel The output of the above code will look like this: And the Excel file Sample.xlsx will look like this: In the above example, we see that we can easily parse an Excel file into a DataSet and employ it with every WorkSheet of an Excel file as a DataTable. Dive more into how to parse Excel as a DataSet here featuring code examples. Let's see one more example of how to access each cell value of all ExcelSheets. Here, we can access each cell value of every WorkSheet of an Excel file. :path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-all-excel-sheets.cs // 该代码片段不可用! IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel Using the above example, it is very convenient to access each cell value of every WorkSheet of an Excel file. For more on how to Read Excel Files Without Interop check out the code here. Tutorial Quick Access API Reference for IronXL Read more about IronXL's features, classes, method fields, namespaces, and enums in the documentation. API Reference for IronXL 常见问题解答 如何在 C# 中读取 XLSX 文件而不使用 Interop? 要在 C# 中读取 XLSX 文件而不使用 Interop,可以使用 IronXL 库。安装 IronXL 后,可以使用 WorkBook.Load() 方法加载 Excel 工作簿并访问其内容。 如何在 C# 中加载 Excel 工作簿? 可以在 C# 中通过调用 WorkBook.Load('yourfile.xlsx') 使用 IronXL 加载 Excel 工作簿,这将打开指定的文件以供操作。 有哪些方法可以用于访问 Excel 文件中的工作表? 在 IronXL 中,可以通过名称或索引使用 GetWorkSheet 方法以及 DefaultWorkSheet 或 First 等属性访问工作表。 如何用 C# 操作 Excel 工作表中的数据? 可以在 C# 中使用 IronXL 操作 Excel 工作表中的数据,通过 ws['A1'].ToString() 这样的语法访问单元格值(用于字符串),或 ws['B1'].Int32Value(用于整数),并在单元格范围内应用函数如 Sum。 可以在 C# 中对 Excel 数据进行聚合操作吗? 是的,IronXL 允许在 C# 中对 Excel 数据进行聚合操作,使用像 Sum()、Min() 和 Max() 这样的方法在指定的单元格范围内进行。 能否将 Excel 工作表转换为 DataTable? 是的,可以在 C# 中使用 IronXL 的 ToDataTable() 方法将 Excel 工作表转换为 DataTable,并且可以选择将第一行作为列名处理。 如何在 C# 中将 Excel 工作簿用于 DataSet? 使用 IronXL,可以通过调用 ToDataSet() 方法将 Excel 工作簿用作 DataSet,从而实现全面的数据操作。 在 C# 中使用库进行 Excel 文件操作有哪些优势? 使用 IronXL 在 C# 中进行 Excel 文件操作提供了简便和高效的优点,支持各种 Excel 格式,能够对数据进行操作,并提供将工作表转换为 DataTable 等功能。 在哪里可以找到更多关于使用 IronXL 的资源? 您可以在 IronXL 的 API 参考页面找到更多资源和文档,其中包含有关其功能、类和方法的详细信息。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,686,155 查看许可证