使用 IRONXL 如何在 C# 中将 Excel 转换为 DataTable 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 blog demonstrates how to implement an Excel to DataTable conversion. This task can be done easily using IronXL, which provides a simple and efficient way to read and write Excel files in C#. 1. IronXL IronXL is a powerful and user-friendly C# library designed to simplify the reading, writing, exporting data, and manipulation of Excel worksheets. It provides developers with a seamless way to work with Excel spreadsheets and automate tasks within their C# projects. With IronXL, you can easily import, export, and modify data in Excel workbooks, worksheets, and cells. It supports both the older XLS format and the newer XLSX format, making it compatible with a wide range of Excel versions. The library offers a straightforward API that enables developers to perform various operations on Excel files, such as creating new workbooks, reading and writing data to cells, applying formatting, handling formulas, and even extracting charts and images. IronXL also supports working with complex data structures like tables, ranges, and named ranges. 2. Prerequisites To work with Excel files and convert them to DataTable using IronXL in C#, there are a few prerequisites that need to be in place: Development Environment: Ensure that you have a compatible development environment set up on your machine, such as Visual Studio or any other C# IDE. IronXL Library: Download and install the IronXL library. You can obtain the library from the official IronXL website or via the NuGet Package Manager in Visual Studio. Include the IronXL namespace in your C# project to access its classes and methods. .NET Framework or .NET Core: IronXL supports both the .NET Framework and .NET Core. Ensure that you have the appropriate version installed on your machine, depending on your project requirements. 3. Creating a New Project in C# To use the IronXL library for Excel-related tasks, the initial step is to create a .NET project in Visual Studio. While any version of Visual Studio can be used, it's recommended to use the latest one. This tutorial will use the Console Application project template to showcase how to work with IronXL. New Project Once you have selected the project type, proceed to specify a name and location for the project. Furthermore, you have the option to choose the preferred framework for the project, such as .NET Core 6. Project Configuration After the solution is generated, you can access the Program.cs file, where you can input code and create/execute the application. Program.cs 4. Installing IronXL The IronXL library can be downloaded and installed in different ways. These are: Using Visual Studio NuGet Packages Using the Visual Studio Command Line 4.1 Using Visual Studio NuGet Packages To install the IronXL library with NuGet Package Manager in Visual Studio, 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 use the IronXL library in your project. The screenshot below shows how to open the NuGet Package Manager in Visual Studio. NuGet Package Manager IronXL in search results: IronXL 4.2 Using the Visual Studio Command Line Many developers prefer to install packages using the command line interface. To install IronXL using the command line, follow these steps: Go to Tools > NuGet Package Manager > Package Manager Console in Visual Studio. Enter the following line in the Package Manager Console tab: Install-Package IronXL.Excel Now the package will download and install to the current project and will be ready for use. Install the IronXL package 5. Convert Excel Data to DataTable Using IronXL Exporting Excel data from an Excel file stream to DataTable in C# can be easily implemented using IronXL. This section will discuss how you can convert Excel worksheet data to DataTable using IronXL. First, a sample Excel file is required with the data you want to convert into a DataTable. For demonstration, a simple Excel file should be created. Sample Excel Now let's look at the code samples: using IronXL; using System; using System.Data; class Program { static void Main() { // Load the Excel workbook from a file WorkBook workBook = WorkBook.Load("sample.xlsx"); // Access the default worksheet in the workbook WorkSheet workSheet = workBook.DefaultWorkSheet; // Convert the worksheet data to a DataTable // Set the parameter to true to consider the first row as column headers DataTable dataTable = workSheet.ToDataTable(true); // Iterate over each row in the DataTable foreach (DataRow row in dataTable.Rows) { // Print each column in the row for (int i = 0; i < dataTable.Columns.Count; i++) { Console.Write($"{row[i]} \t"); } Console.WriteLine(); // Move to the next line after printing each row } } } using IronXL; using System; using System.Data; class Program { static void Main() { // Load the Excel workbook from a file WorkBook workBook = WorkBook.Load("sample.xlsx"); // Access the default worksheet in the workbook WorkSheet workSheet = workBook.DefaultWorkSheet; // Convert the worksheet data to a DataTable // Set the parameter to true to consider the first row as column headers DataTable dataTable = workSheet.ToDataTable(true); // Iterate over each row in the DataTable foreach (DataRow row in dataTable.Rows) { // Print each column in the row for (int i = 0; i < dataTable.Columns.Count; i++) { Console.Write($"{row[i]} \t"); } Console.WriteLine(); // Move to the next line after printing each row } } } Imports Microsoft.VisualBasic Imports IronXL Imports System Imports System.Data Friend Class Program Shared Sub Main() ' Load the Excel workbook from a file Dim workBook As WorkBook = WorkBook.Load("sample.xlsx") ' Access the default worksheet in the workbook Dim workSheet As WorkSheet = workBook.DefaultWorkSheet ' Convert the worksheet data to a DataTable ' Set the parameter to true to consider the first row as column headers Dim dataTable As DataTable = workSheet.ToDataTable(True) ' Iterate over each row in the DataTable For Each row As DataRow In dataTable.Rows ' Print each column in the row For i As Integer = 0 To dataTable.Columns.Count - 1 Console.Write($"{row(i)} " & vbTab) Next i Console.WriteLine() ' Move to the next line after printing each row Next row End Sub End Class $vbLabelText $csharpLabel In the above code, an Excel file is loaded as a WorkBook object using the WorkBook.Load method provided by IronXL and then accesses its first worksheet data as a WorkSheet. To convert this WorkSheet to a DataTable, the workSheet.ToDataTable method is used to convert and store the data into a new DataTable variable. Finally, the DataTable data is printed to the console for visualization. Export Data to DataTable Output 6. Conclusion The use of DataTable in C# provides a powerful and efficient solution for working with Excel data within applications. By leveraging libraries like IronXL, developers can easily convert Excel data into DataTable and unlock the full potential of C#. This tutorial explored the capabilities of IronXL and its seamless integration with C#. We discussed the prerequisites for working with Excel files, such as having a compatible development environment, installing the IronXL library, and ensuring the appropriate .NET Framework or .NET Core version. We also walked through the process of creating a new C# project in Visual Studio and installing IronXL either through the NuGet Package Manager or the Visual Studio Command Line. With IronXL successfully integrated into the project, we demonstrated exporting data to a DataTable using the library's easy-to-use API. By loading an Excel file, accessing the desired worksheet, and utilizing the ToDataTable method provided by IronXL, we were able to extract the data and store it in a DataTable. Finally, we showcased how to visualize the DataTable by printing its contents in the console. Using IronXL and DataTable in C#, developers can build robust applications that seamlessly interact with Excel data, enabling efficient data processing, analysis, and visualization. With the knowledge gained from this tutorial, you can now confidently incorporate Excel worksheets into your C# projects and take advantage of the extensive functionality provided by DataTable. For more tutorials on IronXL, please visit the tutorial pages. 常见问题解答 如何在不使用 Interop 的情况下,将 Excel 转换为 C# 中的 DataTable? 您可以使用 IronXL 将 Excel 数据转换为 C# 中的 DataTable。使用 WorkBook.Load 加载您的 Excel 文件,访问所需的工作表,然后使用 workSheet.ToDataTable 执行转换。 在 C# 项目中设置 IronXL 进行 Excel 操作的步骤是什么? 要在您的 C# 项目中设置 IronXL,确保已安装 Visual Studio,然后使用 NuGet 包管理器搜索并安装 IronXL,或使用命令行 Install-Package IronXL.Excel。 IronXL 可以同时用于 .NET Framework 和 .NET Core 吗? 是的,IronXL 兼容 .NET Framework 和 .NET Core,在不同的开发环境中灵活操作 Excel 数据。 IronXL 是否支持 Excel 文件的 XLS 和 XLSX 格式? IronXL 支持 XLS 和 XLSX 两种格式,使其在处理各种 Excel 文件版本时具有多功能性。 使用 IronXL 进行 C# 中 Excel 数据处理的优势是什么? IronXL 提供易于使用的 API 用于读取、写入和操作 C# 中的 Excel 文件。它简化了数据导入/导出的过程,并支持高级特性,如格式化和公式处理。 如何解决使用 IronXL 将 Excel 转换为 DataTable 时的常见问题? 确保 IronXL 已正确安装并在项目中引用。检查您的 Excel 文件路径是否正确,并确保文件未被其他进程使用。查阅 IronXL 文档以获取特定的错误信息。 推荐使用哪种开发环境来使用 IronXL? 推荐使用 Visual Studio 来使用 IronXL,因为它提供了强大的开发工具和无缝集成的 C# 项目。 如何在 C# 中可视化从 Excel 文件获得的 DataTable 内容? 在使用 IronXL 将 Excel 数据转换为 DataTable 后,您可以在 C# 中遍历 DataTable 行和列,将内容打印到控制台以进行可视化。 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读取器。 阅读更多 如何在 Blazor 中导出到 CSV如何在 .NET 6 中导出文件到...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多