使用 IRONXL 如何在 C# 中将 Excel 转换为 DataGridView 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 Library Features Using the IronXL library in C#, you can easily read and convert Microsoft Excel documents to CSV files. IronXL is a standalone .NET software library that can read various spreadsheet formats without requiring the installation of Microsoft Excel or depending on Microsoft Office Interop Excel. IronXL is one of the leading Excel spreadsheet libraries for C#, compatible with both .NET Core and .NET Framework. It supports various .NET Frameworks, including Web Applications, Windows Forms applications, and Console Applications. Reading Excel files is fast and straightforward with IronXL. It supports a wide range of Excel file formats, including XLSX, XLS, CSV, TSV, XLST, XLSM, and others. You can import, edit, export data tables, export datasets, and perform many other operations. Exporting and saving files with different extensions is also possible. With IronXL's intuitive C# API, you can effortlessly read, modify, and create Excel spreadsheet files in the .NET environment. It provides comprehensive support for .NET Core, .NET Framework, Xamarin, Mobile, Linux, macOS, and Azure. IronXL can perform calculations like Microsoft Excel can and supports several column data formats, such as text, integers, formulas, dates, currencies, and percentages. To learn more, click on the IronXL website. Creating a New Project in Visual Studio Before using the IronXL framework, a .NET project must be created in Visual Studio. Any version of Visual Studio will work; however, using the most recent version is recommended. You can create various project templates or a Windows Forms-like application, depending on your requirements. This tutorial will be using the Windows Forms Application to simplify things. Create a new project in Visual Studio After choosing your project template, provide the name and location of the project. Configure the project Next, pick the following structure. This project will make use of .NET Framework 4.7. .NET Framework selection After generating the project files, you can open the Form1.cs file in the designer view to insert the program code, design the interface, and build/run the program. Then, you will need to download the required IronXL library for the solution. You can download the package by using the following command in the package manager: Install-Package IronXL.Excel Install the IronXL package in the Package Manager Console Alternatively, you can use the NuGet Package Manager to search for and download the "IronXL" package. The NuGet Package Manager provides a convenient way to manage dependencies in your project. Search and install the IronXL package in NuGet Package Manager UI For this form, a DataGridView control is placed on the form to load the necessary Excel file into the DataGridView control. Loading Data from an Excel File IronXL is a powerful library for Excel that allows you to read Excel files with just a few lines of code. Below is an example code snippet that demonstrates how to import Excel data into a DataGridView: using System; using System.Windows.Forms; using IronXL; namespace DataTableWindowsForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string importFilePath = string.Empty; openFileDialog1.Filter = "Excel (*.xlsx)|*.xlsx"; DialogResult result = openFileDialog1.ShowDialog(); // Check if the user selected a file if (result == DialogResult.OK) { importFilePath = openFileDialog1.FileName; } // Proceed if the file path is not empty if (!string.IsNullOrEmpty(importFilePath)) { // Load the Excel document WorkBook excelDoc = WorkBook.Load(importFilePath); // Convert the first sheet to a DataTable var dataTable = excelDoc.WorkSheets[0].ToDataTable(); // Set the DataSource of dataGridView1 to the DataTable dataGridView1.DataSource = dataTable; } } } } using System; using System.Windows.Forms; using IronXL; namespace DataTableWindowsForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string importFilePath = string.Empty; openFileDialog1.Filter = "Excel (*.xlsx)|*.xlsx"; DialogResult result = openFileDialog1.ShowDialog(); // Check if the user selected a file if (result == DialogResult.OK) { importFilePath = openFileDialog1.FileName; } // Proceed if the file path is not empty if (!string.IsNullOrEmpty(importFilePath)) { // Load the Excel document WorkBook excelDoc = WorkBook.Load(importFilePath); // Convert the first sheet to a DataTable var dataTable = excelDoc.WorkSheets[0].ToDataTable(); // Set the DataSource of dataGridView1 to the DataTable dataGridView1.DataSource = dataTable; } } } } Imports System Imports System.Windows.Forms Imports IronXL Namespace DataTableWindowsForm Partial Public Class Form1 Inherits Form Public Sub New() InitializeComponent() End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim importFilePath As String = String.Empty openFileDialog1.Filter = "Excel (*.xlsx)|*.xlsx" Dim result As DialogResult = openFileDialog1.ShowDialog() ' Check if the user selected a file If result = System.Windows.Forms.DialogResult.OK Then importFilePath = openFileDialog1.FileName End If ' Proceed if the file path is not empty If Not String.IsNullOrEmpty(importFilePath) Then ' Load the Excel document Dim excelDoc As WorkBook = WorkBook.Load(importFilePath) ' Convert the first sheet to a DataTable Dim dataTable = excelDoc.WorkSheets(0).ToDataTable() ' Set the DataSource of dataGridView1 to the DataTable dataGridView1.DataSource = dataTable End If End Sub End Class End Namespace $vbLabelText $csharpLabel The above code imports data from Excel. To provide more context, let's assume we are designing a Windows Forms application with a DataGridView control and a button. The button will open a dialog to browse for the Excel file from a specific location. A custom code is added to filter and display only a limited number of files with a specific extension. Open a dialog to select an Excel file Then, the Load method is used to load data from the Excel file. This method allows loading data from various sources such as filenames, bytes, or streams. If the Excel file is password-protected, you can also pass the password as a parameter. Once the Excel details are loaded into the IronXL object, the ToDataTable method is used to convert the Excel sheet data into a DataTable. Each sheet in the Excel file will be converted into a separate table. Then, load the required table from the dataset into the DataGridView. To do this, the DataGridView's DataSource property is set to the newly created DataTable object from IronXL. This step allows the data population from the Excel file to the DataGridView. Display data from the Excel file Additionally, you may need to configure the DataGridView so it properly displays headers and the first row of data, depending on your specific requirements. For more tutorials on using IronXL, click on this how-to guide. Conclusion IronXL is a highly popular Excel utility that does not rely on external libraries. It is an independent solution and does not require the installation of Microsoft Excel. It is compatible with various platforms. With IronXL, you can accomplish a wide range of operations related to Microsoft Excel documents programmatically. You can perform tasks such as sorting strings or numbers, trimming and adding data, finding and replacing values, merging and unmerging cells, saving files, and more. It also allows you to validate spreadsheet data and define cell data types. Additionally, IronXL supports reading and writing CSV files. IronXL is available for purchase with different licensing options. Customers also have the option to pay an annual membership fee for updates and product support. For an additional charge, IronXL provides unrestricted redistribution rights. To obtain more detailed pricing information, you can visit the licensing page. 常见问题解答 如何在 C# 中不使用 Interop 显示 DataGridView 中的 Excel 数据? 您可以使用 IronXL 来读取 Excel 数据并通过将 Excel 表转换为 DataTable 然后设置为 DataGridView 的数据源来显示它。 在 C# 中使用 IronXL 进行 Excel 操作的好处是什么? IronXL 提供了一个独立的解决方案进行 Excel 操作,无需 Microsoft Excel 或 Office Interop。它支持多种平台和格式,使其在不同的 C# 应用程序中显得非常灵活。 如何在 Visual Studio 项目中安装 IronXL 以进行 Excel 处理? 通过在包管理器控制台中运行 Install-Package IronXL.Excel 或通过 Visual Studio 中的 NuGet 包管理器 UI 安装 IronXL。 IronXL 是否支持读取和写入不同的 Excel 文件格式? 是的,IronXL 支持读取和写入多种 Excel 文件格式,包括 XLSX、XLS、CSV 和 TSV。 IronXL 可以用于跨平台应用程序吗? 是的,IronXL 兼容 .NET Core、.NET Framework、Xamarin 和其他平台,非常适合于跨平台应用程序。 使用 IronXL 将 Excel 数据转换为 DataTable 的过程是什么? 要使用 IronXL 将 Excel 数据转换为 DataTable,请将 Excel 文件读取到 WorkBook 对象中,访问所需的表,并使用 IronXL 的方法将数据提取到 DataTable。 IronXL 是在 C# 中进行 Excel 数据处理的经济有效的解决方案吗? IronXL 提供灵活的许可选项,包括年度会员和再分发权限,使其成为在 C# 中进行 Excel 数据处理的经济有效的解决方案。 IronXL 可以对 Excel 文件执行哪些操作? IronXL 能够执行多种操作,比如读取、写入、排序、修整和合并单元格,还可以处理类似于 Microsoft Excel 的计算和格式化操作。 IronXL 如何处理用户界面的 Excel 数据? IronXL 可以从各种来源加载 Excel 数据,并将其转换为 DataTable,以促进无缝集成到如 Windows 窗体应用程序的 DataGridView 等用户界面。 在哪里可以找到有关使用 IronXL 进行 Excel 操作的更多资源? 在 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# 中从 DataTable 导出大量数据到 Excel如何在 Blazor 中导出到 CSV
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多