使用 IRONXL 如何在 C# 中将 DataTable 导出到 CSV 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 popular library that allows developers to interact with Microsoft Excel documents in C# .NET technologies, including CSV files, without needing Microsoft Excel installed. It enables the automatic conversion of registered types to CSV files and the writing of CSV files with custom structures. IronXL Library Features Microsoft Excel documents can be read and converted to CSV files using the C# IronXL .NET library. IronXL is a standalone .NET software library that can read a variety of spreadsheet formats. It does not depend on Microsoft.Office.Interop.Excel or require the installation of Microsoft Excel. With the help of the user-friendly C# API of IronXL, you can quickly read, modify, and create Excel spreadsheet files in the .NET environment. .NET Core, .NET Framework, Xamarin, Mobile, Linux, macOS, and Azure are all fully supported by IronXL. Leading .NET Core and .NET Framework Excel spreadsheet libraries for C# include IronXL. Virtually all of the .NET Frameworks, including the Console, Windows Forms, and Web, are supported by IronXL. IronXL operates on a variety of operating systems, including Windows, Linux, and macOS. IronXL makes it simple and quick to read Excel files. IronXL supports reading different Excel file formats including XLSX files, XLS, CSV, TSV, XLST, XLSM files, and more. We can also load, modify, export Datatables, export datasets, and more. IronXL can export and save files with a wide range of suffixes, including XLS, CSV, TSV, JSON, and others. IronXL can generate Excel calculations. IronXL supports a variety of Excel column data formats, including text, numbers, formulas, dates, currencies, and percentages. For more details visit here. 1. Creating a New Project in Visual Studio In Visual Studio, a .NET project must be created before the IronXL framework can be used. Any edition of Visual Studio will work, but the most recent one is advised. Depending on your needs, you can build a Windows Forms-like application or different project templates. To keep things simple, this lesson will use the Console Application. Create a new project in Visual Studio After that, input the project's name and location. Configure the new project Select the following structure next. .NET Core 6 will be used in this undertaking. Select a .NET Framework version The program.cs file will be opened after the application generates the solution so that you can enter the program code and build/run the application. The newly created Console Application project The library can then be added and used to evaluate the code. 2. Install the IronXL Library There are four methods to download and install the IronXL Library. Which are: Installing via Visual Studio Installing using the Visual Studio Package Manager Console Downloading directly from the NuGet website Downloading directly from the IronXL website Installing IronXL using Visual Studio Using NuGet Package Manager, the IronXL module can be installed. To find IronXL, you must first launch the NuGet Package Manager and then look in the browse pane. Install IronXL by choosing it from the search listings. After that, the IronXL library will be able to use this app. The image below demonstrates how to launch Visual Studio's NuGet Package Manager. Navigate to NuGet Package Manager Install IronXL package in NuGet Package Manager UI Installing IronXL using the Visual Studio NuGet Package Manager Console Many individuals enjoy using a console to carry out tasks. So, a terminal installation is also an option. To install IronXL using the command line, adhere to the instructions below. Navigate to Tools > NuGet Package Manager > Package Manager interface in Visual Studio. Input the following command into the console tab of the package manager: Install-Package IronXL.Excel Wait for IronXL to be downloaded and installed into the active project. Install the IronXL package in Package Manager Console UI Downloading IronXL Directly from the NuGet Website The NuGet package can be downloaded straight from the website as a third option. Explore the official NuGet link. The download package choice can be found in the menu on the right. Click the saved file twice. It will immediately be installed. Reload the solution after that and begin utilizing it in the project. Getting directly from the IronXL website To download the most recent package straight from the website, click this link to download an IronXL ZIP file. This link will download a ZIP file containing the latest version of the IronXL library DLL. Once the download finishes, extract the contents of the ZIP file to any directory of your choosing. To add the file to the project after downloading, adhere to the steps listed below. From the solution window, right-click the project. Select References, and then navigate to the extracted folder containing the IronXL DLLs. Select the DLL, and click OK to add it to the active project as a Reference. 3. Export to CSV File DataTables can be easily and quickly created to CSV files using IronXL. It helps write data into a new CSV file. First, as shown in the code image below, the IronXL namespace should be included to use the IronXL classes and methods. Add common namespaces Excel files can be created using IronXL, which then transforms them into WorkBook objects. Then conduct a variety of operations on them after turning them into objects. The sample code below will build an Excel file by converting a DataTable into an Excel worksheet. using IronXL; using System.Data; // Entry point of the application static void Main(string[] args) { // Specify the file path for the CSV file output ExportToExcel("H:\\test.csv"); } // Exports the DataTable to an Excel file and saves it as CSV public static void ExportToExcel(string filepath) { // Create a DataTable and add columns and rows DataTable table = new DataTable(); table.Columns.Add("DataSet_Fruits", typeof(string)); table.Rows.Add("Apple"); table.Rows.Add("Orange"); table.Rows.Add("Strawberry"); table.Rows.Add("Grapes"); table.Rows.Add("Watermelon"); table.Rows.Add("Bananas"); table.Rows.Add("Lemons"); // Create a new WorkBook and add the DataTable data to it WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS); var writer = wb.DefaultWorkSheet; int rowCount = 1; foreach (DataRow row in table.Rows) { // Write each item from the DataTable into the worksheet starting from cell A1 writer["A" + (rowCount)].Value = row[0].ToString(); rowCount++; } // Save the workbook as a CSV file with a specified delimiter wb.SaveAsCsv(filepath, ";"); } using IronXL; using System.Data; // Entry point of the application static void Main(string[] args) { // Specify the file path for the CSV file output ExportToExcel("H:\\test.csv"); } // Exports the DataTable to an Excel file and saves it as CSV public static void ExportToExcel(string filepath) { // Create a DataTable and add columns and rows DataTable table = new DataTable(); table.Columns.Add("DataSet_Fruits", typeof(string)); table.Rows.Add("Apple"); table.Rows.Add("Orange"); table.Rows.Add("Strawberry"); table.Rows.Add("Grapes"); table.Rows.Add("Watermelon"); table.Rows.Add("Bananas"); table.Rows.Add("Lemons"); // Create a new WorkBook and add the DataTable data to it WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS); var writer = wb.DefaultWorkSheet; int rowCount = 1; foreach (DataRow row in table.Rows) { // Write each item from the DataTable into the worksheet starting from cell A1 writer["A" + (rowCount)].Value = row[0].ToString(); rowCount++; } // Save the workbook as a CSV file with a specified delimiter wb.SaveAsCsv(filepath, ";"); } Imports IronXL Imports System.Data ' Entry point of the application Shared Sub Main(ByVal args() As String) ' Specify the file path for the CSV file output ExportToExcel("H:\test.csv") End Sub ' Exports the DataTable to an Excel file and saves it as CSV Public Shared Sub ExportToExcel(ByVal filepath As String) ' Create a DataTable and add columns and rows Dim table As New DataTable() table.Columns.Add("DataSet_Fruits", GetType(String)) table.Rows.Add("Apple") table.Rows.Add("Orange") table.Rows.Add("Strawberry") table.Rows.Add("Grapes") table.Rows.Add("Watermelon") table.Rows.Add("Bananas") table.Rows.Add("Lemons") ' Create a new WorkBook and add the DataTable data to it Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS) Dim writer = wb.DefaultWorkSheet Dim rowCount As Integer = 1 For Each row As DataRow In table.Rows ' Write each item from the DataTable into the worksheet starting from cell A1 writer("A" & (rowCount)).Value = row(0).ToString() rowCount += 1 Next row ' Save the workbook as a CSV file with a specified delimiter wb.SaveAsCsv(filepath, ";") End Sub $vbLabelText $csharpLabel The above code exports the DataTable to an Excel file. Column headings are created once a DataTable is created. Then, add the rows one at a time after establishing the first column. The WorkBook object is created after adding the columns and rows to the DataTable object to hold those data. The WorkSheet object is then constructed, which is added to the WorkBook object. Each value from the DataTable is read and added using a foreach loop before adding the value to the WorkSheet. After all the values have been added to the worksheet, the SaveAsCsv method is used to save them into a CSV file; at the same time, we can also give the delimiter and file name with location as parameters. The output CSV file The output of the run code sample is shown above. In the screenshot, each piece of data from the data table has been individually added to the newly formed Excel sheet. To learn more about the IronXL tutorial click on this how-to export to Excel formats. Conclusion One of the most popular Excel tools is IronXL. It doesn't rely on any other libraries from outside sources. It is autonomous and does not require the installation of Microsoft Excel. It operates across numerous channels. IronXL provides an all-in-one solution for all Microsoft Excel document-related tasks to be implemented programmatically. You can perform formula calculation, string or number sorting, trimming and appending, find and replace, merge and unmerge, save files etc. You can also set cell data formats along with validate spreadsheet data. It also supports reading and writing CSV files and helps you to work like Excel data. IronXL's starting price at launch is $799. It also offers users the choice of paying a one-year subscription fee for product assistance and updates. For an additional fee, IronXL offers security for unrestricted redistribution. To research greater approximate pricing information, please visit this licensing page. 常见问题解答 我如何在 C# 中将 DataTable 导出到 CSV 文件? 您可以使用 IronXL 通过创建 WorkBook 并将数据导出到工作表对象来将 DataTable 导出为 CSV 文件。这允许无缝转换和导出,而无需 Microsoft Excel。 我可以使用此库导出哪些文件格式? IronXL 允许将数据导出为多种格式,如 XLS、CSV、TSV 和 JSON,提供了处理不同数据需求的灵活性。 是否可以使用此库执行类似 Excel 的操作? 是的,IronXL 支持类似 Excel 的操作,如公式计算、数据排序和文件保存,使其成为与 Excel 相关的编程任务的综合工具。 我需要安装 Microsoft Excel 才能使用此库吗? 不,IronXL 是一个独立的 .NET 库,不需要安装 Microsoft Excel,开发人员可以直接在他们的 C# 应用程序中操作 Excel 文档。 哪些平台与此库兼容? IronXL 与多个平台兼容,包括 .NET Core、.NET Framework、Xamarin,以及操作系统如 Windows、Linux 和 macOS。 如何在我的项目中安装 IronXL 库? 可以通过访问 NuGet 包管理器或直接从 NuGet 或 IronXL 网站下载来在 Visual Studio 中将 IronXL 安装到 .NET 项目中。 有哪些可用的定价选项? 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读取器。 阅读更多 如何从 Excel 文件中获取单元格值在 C# 中如何在 C# 中导入 Excel 文件
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多