使用 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 Working with Excel files is a common task in software development, especially when dealing with data manipulation and analysis. Reading Excel files in a C# console application is a common task in many business and data processing scenarios. Excel files, with their tabular data structure, are frequently used to store and exchange information. In C# applications, IronXL provides a powerful and user-friendly solution for handling Excel files. This article will guide you through the process of reading Excel files in a C# Console Application using the IronXL library. How to Read an Excel File in a C# Console Application Create a C# Console Application in Visual Studio Install the IronXL C# Excel Library Create a Workbook class Object Load the Excel file using Workbook.Load method Load the Worksheet using the WorkSheets method Read the Excel File data using WorkSheet.Row method Loop through the Cell values Print the Cell.Text on Console Window Introduction to IronXL Library IronXL is a .NET library designed to simplify Excel-related tasks for developers. Whether you need to create, modify, or read Excel files, IronXL offers a comprehensive set of features and functionalities. IronXL simplifies the process of interacting with Excel workbooks, sheets, and cells. With IronXL, developers can effortlessly read and write data to Excel files, enabling seamless integration of Excel functionality into C# projects without Microsoft Office Interop or Excel installed. By leveraging the capabilities of IronXL, developers can manipulate cell values, extract data from an Excel Workbook, and generate Excel documents dynamically. With its intuitive API and robust feature set, IronXL empowers developers to efficiently handle Excel data, making tasks such as data analysis, reporting, and document generation a breeze. Whether you're working with Microsoft Excel files, spreadsheets, or worksheets, IronXL provides the tools you need to streamline your C# application development process. Features of IronXL Before moving forward, let's highlight some key features of IronXL: Create and Edit Excel Files: IronXL supports the creation and modification of Excel files, allowing developers to manipulate worksheets, cells, and formatting. Read Excel Files: The library facilitates the extraction of data from existing Excel files, making it easy to read and process Excel spreadsheet data within .NET applications. Export to Excel: IronXL enables the exporting of data from your application to an Excel format, ensuring compatibility with other tools and platforms. Formulas and Functions: Support for Excel formulas and functions allows dynamic calculations and data manipulation. Cell Formatting: IronXL provides features for formatting cells, including styles, fonts, colors, and borders. Creating Console Application using Visual Studio Let's start by creating a new C# Console Application in Visual Studio. Open Visual Studio. Select "Create a New Project". Choose "Console App" under C# templates. Provide a name for your project and click "Next." Set the appropriate .NET Framework and click "Create." Install IronXL using the NuGet Package Manager Console or Solutions Now that we have our console application, we need to install the IronXL library. Option 1: Using the NuGet Package Manager Console Install-Package IronXL.Excel Option 2: Using the NuGet Package Manager in Visual Studio Right-click on your project in Solution Explorer. Select "Manage NuGet Packages." Search for "IronXL" and click "Install." Steps to Read Excel Files using IronXL Now, let's go through the steps to read an Excel file using IronXL in our C# Console Application. The following code snippet allows you to read Excel files in the C# console application: using IronXL; class Program { public static void Main() { // Specify the path to the Excel file string excelFilePath = "path/to/your/excel/file.xlsx"; // Create a WorkBook object by loading an existing Excel file WorkBook workBook = WorkBook.Load(excelFilePath); // Access the first worksheet in the workbook WorkSheet workSheet = workBook.WorkSheets[0]; // Iterate through each row in the worksheet foreach (var row in workSheet.Rows) { // Iterate through each cell in the row foreach (var cell in row) { // Print the text of the cell followed by a tab Console.Write(cell.Text + "\t"); } // Move to the next line after each row Console.WriteLine(); } // Close the workbook to free resources workBook.Close(); } } using IronXL; class Program { public static void Main() { // Specify the path to the Excel file string excelFilePath = "path/to/your/excel/file.xlsx"; // Create a WorkBook object by loading an existing Excel file WorkBook workBook = WorkBook.Load(excelFilePath); // Access the first worksheet in the workbook WorkSheet workSheet = workBook.WorkSheets[0]; // Iterate through each row in the worksheet foreach (var row in workSheet.Rows) { // Iterate through each cell in the row foreach (var cell in row) { // Print the text of the cell followed by a tab Console.Write(cell.Text + "\t"); } // Move to the next line after each row Console.WriteLine(); } // Close the workbook to free resources workBook.Close(); } } Imports Microsoft.VisualBasic Imports IronXL Friend Class Program Public Shared Sub Main() ' Specify the path to the Excel file Dim excelFilePath As String = "path/to/your/excel/file.xlsx" ' Create a WorkBook object by loading an existing Excel file Dim workBook As WorkBook = WorkBook.Load(excelFilePath) ' Access the first worksheet in the workbook Dim workSheet As WorkSheet = workBook.WorkSheets(0) ' Iterate through each row in the worksheet For Each row In workSheet.Rows ' Iterate through each cell in the row For Each cell In row ' Print the text of the cell followed by a tab Console.Write(cell.Text & vbTab) Next cell ' Move to the next line after each row Console.WriteLine() Next row ' Close the workbook to free resources workBook.Close() End Sub End Class $vbLabelText $csharpLabel This C# code snippet demonstrates how to use the IronXL library to read data from an Excel file and display it in a console application. The following Excel file will be read and displayed on the console window: Now, let's break down the code step by step: Importing the IronXL Namespace using IronXL; using IronXL; Imports IronXL $vbLabelText $csharpLabel This line imports the IronXL namespace, which contains classes and methods for working with any Excel document. Main Method public static void Main() { // Main method where the program execution begins } public static void Main() { // Main method where the program execution begins } Public Shared Sub Main() ' Main method where the program execution begins End Sub $vbLabelText $csharpLabel The Main method is the entry point of the application. We will write the above code snippet in this main method. Excel File Path string excelFilePath = "path/to/your/excel/file.xlsx"; string excelFilePath = "path/to/your/excel/file.xlsx"; Dim excelFilePath As String = "path/to/your/excel/file.xlsx" $vbLabelText $csharpLabel This line specifies the path to the Excel file that you want to read. Replace "path/to/your/excel/file.xlsx" with the actual path to your Excel file. Load Excel File WorkBook workBook = WorkBook.Load(excelFilePath); WorkBook workBook = WorkBook.Load(excelFilePath); Dim workBook As WorkBook = WorkBook.Load(excelFilePath) $vbLabelText $csharpLabel The WorkBook.Load() method is used to load the Excel file specified by excelFilePath into a WorkBook (Excel) object named workBook. Accessing Worksheet WorkSheet workSheet = workBook.WorkSheets[0]; WorkSheet workSheet = workBook.WorkSheets[0]; Dim workSheet As WorkSheet = workBook.WorkSheets(0) $vbLabelText $csharpLabel This line accesses the first Excel sheet in the workbook (workBook.WorkSheets [0]) and assigns it to an Excel WorkSheet object named workSheet. Iterating Through Rows and Columns foreach (var row in workSheet.Rows) { foreach (var cell in row) { Console.Write(cell.Text + "\t"); } Console.WriteLine(); } foreach (var row in workSheet.Rows) { foreach (var cell in row) { Console.Write(cell.Text + "\t"); } Console.WriteLine(); } Imports Microsoft.VisualBasic For Each row In workSheet.Rows For Each cell In row Console.Write(cell.Text & vbTab) Next cell Console.WriteLine() Next row $vbLabelText $csharpLabel These nested foreach loops iterate through each row and column in the worksheet. For each cell, the cell's text value (cell.Text) is printed to the console, followed by a tab character ("\t"). After printing all cells in a row, a newline character is printed to move to the next row. Closing the Workbook workBook.Close(); workBook.Close(); workBook.Close() $vbLabelText $csharpLabel Finally, the workBook.Close() method is called to close the workbook and release any resources associated with it. For more working code examples please visit this code examples page. Output Upon running the application, the data from Excel file will be printed on the console window: Conclusion In this tutorial, we explored the process of reading Excel files in a C# Console Application using the IronXL library. With its intuitive features, IronXL simplifies Excel-related tasks, making it a valuable tool for .NET developers working on data-centric applications. Feel free to explore additional functionalities provided by IronXL for more advanced Excel manipulations in your C# projects by visiting this documentation page. IronXL offers a free trial for testing out its complete functionality before making an informed decision. For commercial use, you need to purchase a license key starting from $799. For more information on license packages, please visit this license page. Download the library from here and give it a try. 常见问题解答 如何在 C# 控制台应用程序中读取 Excel 文件? 您可以通过使用 IronXL 库在 C# 控制台应用程序中读取 Excel 文件。首先在 Visual Studio 中创建一个控制台应用程序,然后通过 NuGet 安装 IronXL。使用 Workbook.Load 方法加载 Excel 文件、访问工作表,并遍历行和单元格来读取数据。 在 Visual Studio 中设置 IronXL 包含哪些步骤? 要在 Visual Studio 中设置 IronXL,首先创建一个新的 C# 控制台应用程序。然后,打开 NuGet 包管理器控制台并运行 Install-Package IronXL.Excel 来安装库。一旦安装,您就可以开始使用 IronXL 的功能来处理 Excel 文件。 我可以在没有安装 Microsoft Office 的情况下使用 IronXL 处理 Excel 文件吗? 是的,IronXL 允许您在系统上不需要安装 Microsoft Office 或 Excel 的情况下处理 Excel 文件。它独立运行,是一个处理 Excel 任务的方便解决方案。 如何使用 IronXL 访问 Excel 工作表中的特定数据? 在使用 Workbook.Load 加载 Excel 工作簿后,您可以通过 WorkSheets 属性访问特定的工作表。遍历行和单元格以访问特定数据,并使用 GetCellAt 等方法从特定单元格检索数据。 使用 IronXL 进行 Excel 操作在 C# 中有什么优势? IronXL 通过提供易于加载和读取 Excel 文件、支持 Excel 公式以及数据导出功能,简化了 C# 中的 Excel 操作。它还允许格式化、编辑和创建 Excel 文件,无需 Microsoft Office。 如何使用 IronXL 关闭 Excel 工作簿? 您可以通过调用 WorkBook.Close() 方法在 IronXL 中关闭 Excel 工作簿。这有助于释放资源并确保应用程序中高效的内存使用。 在购买许可之前,有办法测试 IronXL 吗? 是的,IronXL 提供了一个具有完整功能的免费试用版本。这允许您评估库并确保满足您的需求,然后再购买商业许可证。 在哪里可以找到 IronXL 的文档和示例? 您可以在其官方网站上找到 IronXL 的全面文档和示例代码。该资源提供了有关在项目中使用 IronXL 功能和能力的详细指导。 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# 中重命名 Excel 工作表如何在 C# 中导出 Datagridview...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多