使用 IRONXL 如何在 .NET Core 中导出到 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 1.0 Introduction One of the most well-known libraries, IronXL, will be used in this article to contrast and compare different ways that .NET technologies can interface programmatically with Microsoft Excel documents. It will also build an environment for reading, writing, and exporting Excel spreadsheets to CSV files. 2.0 IronXL The IronXL for .NET, a C# Excel library, can be used to read and convert Microsoft Excel documents to CSV files. IronXL is a stand-alone .NET software library that may be used without the installation of Microsoft Office or Microsoft.Office.Interop.Excel. It can read a variety of spreadsheet formats. Excel spreadsheets may be read, edited, and generated with ease in a .NET context thanks to IronXL's straightforward C# API. Xamarin, Linux, macOS, Azure, .NET Core, and .NET Framework are all fully supported by IronXL. 2.1 IronXL Library Features Among the greatest C# libraries for Excel spreadsheets is IronXL, which works with both .NET Core and .NET Framework. Virtually all .NET Frameworks are supported by IronXL, including console, Windows Forms, and Web Applications. Windows, macOS, and Linux are all compatible with IronXL. Excel files may be accessed quickly and easily using IronXL. Excel file formats like XLSX files, CSV files, XLS, XSLT, TSV, XLSM, and others, can be read by IronXL. Among many possibilities are functions for importing, updating, and exporting datasets and data tables. Calculations for the Excel spreadsheet can be generated by IronXL. For Excel columns, IronXL supports a wide range of data types, such as text, integers, dates, currencies, formulas, and percentages. Dates, currencies, percentages, text, numbers, formulas, and other Excel column data types are all supported by IronXL. 3.0 Creating a .NET Core 6 Project You will see how easy it is to build a CSV file using the IronXL library in the following sections of this newsletter. Step 1: Start a new project to generate a CSV file. Open Visual Studio and choose "New Project" from the "File" menu. Choose the "Console App" .NET Project templates from the ensuing dialogue box, then click "Next". Create a new Console Application in Visual Studio You can enter whatever name you want for the "Project name". Once the location of the new project has been provided in the "Location" section, click on the Next button to continue. Configure the new project The Framework drop-down menu can be used to choose a .NET Framework. In this case, the long-term supported version of .NET is 6.0. Next, click on the Create button. .NET target framework selection Install the IronXL library, as it is necessary for the subsequent resolution. Enter the following command into the Package Manager Console to accomplish this: Install-Package IronXL.Excel Install the IronXL package An alternative is to search for the package "IronXL" using the NuGet Package Manager. Within the Browse tab, enter "IronXL" in the search box to search for the IronXL library. From this list of all the NuGet packages related to IronXL, then select the required package to download. Search and install the IronXL package in NuGet Package Manager UI 4.0 Export data to CSV file With IronXL, creating data tables to CSV files is simple and rapid. It facilitates writing data to a fresh CSV file. The first step is to include the IronXL namespace, as seen in the code screenshot below. Once IronXL is presented, then its classes and methods can be used in code. Include the IronXL namespace IronXL can be used to create Excel files, which are subsequently converted into WorkBook objects. After they become objects from WorkBook class, it is possible to work on them in several ways. By transforming a DataTable into an Excel worksheet, the sample source code below creates an Excel file. using IronXL; using System.Data; class Program { static void Main(string[] args) { ExportToExcel("test.csv"); } // Method to export a DataTable to an Excel sheet which is then saved as a CSV file public static void ExportToExcel(string filepath) { // Create a DataTable and add columns & rows DataTable table = new DataTable(); table.Columns.Add("DataSet_Animals", typeof(string)); table.Rows.Add("Lion"); table.Rows.Add("Tiger"); table.Rows.Add("Leopard"); table.Rows.Add("Cheetah"); table.Rows.Add("Hyenas"); // Create a new workbook and get the default worksheet var workbook = WorkBook.Create(ExcelFileFormat.XLS); var worksheet = workbook.DefaultWorkSheet; // Add table data to worksheet int rowCount = 1; foreach (DataRow row in table.Rows) { worksheet["A" + rowCount].Value = row[0].ToString(); rowCount++; } // Save worksheet data to a CSV file workbook.SaveAsCsv(filepath, ";"); // Optionally convert workbook to a stream, useful for web applications var stream = workbook.ToStream(); } } using IronXL; using System.Data; class Program { static void Main(string[] args) { ExportToExcel("test.csv"); } // Method to export a DataTable to an Excel sheet which is then saved as a CSV file public static void ExportToExcel(string filepath) { // Create a DataTable and add columns & rows DataTable table = new DataTable(); table.Columns.Add("DataSet_Animals", typeof(string)); table.Rows.Add("Lion"); table.Rows.Add("Tiger"); table.Rows.Add("Leopard"); table.Rows.Add("Cheetah"); table.Rows.Add("Hyenas"); // Create a new workbook and get the default worksheet var workbook = WorkBook.Create(ExcelFileFormat.XLS); var worksheet = workbook.DefaultWorkSheet; // Add table data to worksheet int rowCount = 1; foreach (DataRow row in table.Rows) { worksheet["A" + rowCount].Value = row[0].ToString(); rowCount++; } // Save worksheet data to a CSV file workbook.SaveAsCsv(filepath, ";"); // Optionally convert workbook to a stream, useful for web applications var stream = workbook.ToStream(); } } Imports IronXL Imports System.Data Friend Class Program Shared Sub Main(ByVal args() As String) ExportToExcel("test.csv") End Sub ' Method to export a DataTable to an Excel sheet which is then saved as a CSV file Public Shared Sub ExportToExcel(ByVal filepath As String) ' Create a DataTable and add columns & rows Dim table As New DataTable() table.Columns.Add("DataSet_Animals", GetType(String)) table.Rows.Add("Lion") table.Rows.Add("Tiger") table.Rows.Add("Leopard") table.Rows.Add("Cheetah") table.Rows.Add("Hyenas") ' Create a new workbook and get the default worksheet Dim workbook = WorkBook.Create(ExcelFileFormat.XLS) Dim worksheet = workbook.DefaultWorkSheet ' Add table data to worksheet Dim rowCount As Integer = 1 For Each row As DataRow In table.Rows worksheet("A" & rowCount).Value = row(0).ToString() rowCount += 1 Next row ' Save worksheet data to a CSV file workbook.SaveAsCsv(filepath, ";") ' Optionally convert workbook to a stream, useful for web applications Dim stream = workbook.ToStream() End Sub End Class $vbLabelText $csharpLabel The above CSV example shows how to export the DataTable into a CSV file. After a DataTable has been established, i.e., column headings are created, and once the first column is established, the rows are added one at a time. After adding the rows and columns to the DataTable object, the WorkBook object is constructed. The WorkBook object is used to add data to an Excel sheet, which can then be saved elsewhere. The next step is to initiate the WorkSheet object, which is linked to the workbook object. Before adding the value to the worksheet, a foreach loop is used to read each value from the DataTable. The SaveAsCsv function is used to save the data into a CSV file once they have all been put into the worksheet using the file name as a parameter. A delimiter can be used as an optional argument if it is required. The library then assists in writing data to a CSV file. There is another way to read CSV files besides Microsoft Excel using Notepad. Also, the method Save is used to save the same into the given file format. OUTPUT Excel file test.csv Above is the output of the code sample that was run. Every piece of information from the data table has been separately added to the freshly created Excel sheet in the screenshot. Alternatively, it can also be converted into a stream as part of the Web Application to return files that can be downloaded from the client-side. For more information on exporting data from DataTable to Excel, please check this tutorial page. To know more about how to export data into Excel, please refer to this step-by-step tutorial. 5.0 Conclusion IronXL is among the most widely used Excel utilities. It is not dependent on any other external libraries. It is self-contained and doesn't need Microsoft Excel installed. Furthermore, it functions through a variety of channels. For all programmatically implemented Microsoft Excel document-related operations, IronXL offers a comprehensive solution. Calculating formulas, sorting strings or numbers, trimming, appending, finding and replacing, merging and unmerging, saving files, and more are all possible. Together with validating spreadsheet data, you can also establish cell data types. It facilitates working with Excel data and allows you to read and write files. IronXL offers a free trial license, which allows users to try out and test all its amazing features for free. At launch, IronXL is available for $799. Users can also opt to pay a one-year subscription fee to receive updates and product assistance. IronXL provides security for unrestricted redistribution for an extra charge. To look up more precise pricing data, please visit IronXL's license page. 常见问题解答 如何使用 .NET Core 将数据导出为 CSV 文件? 您可以在 .NET Core 应用程序中使用 IronXL 将数据导出为 CSV 文件。首先,通过 Visual Studio 设置一个新的 .NET Core 项目,通过 NuGet 安装 IronXL,并使用其 WorkBook 和 WorkSheet 对象,通过 SaveAsCsv 方法将数据保存为 CSV 文件。 是否可以在不使用 Microsoft Office 的情况下读取和写入 Excel 文件? 是的,IronXL 允许您在不需要 Microsoft Office 或 Microsoft.Office.Interop.Excel 的情况下读取和写入 Excel 文件。它为 .NET 应用程序中的 Excel 电子表格管理提供了一个独立的解决方案。 在 .NET Core 中使用 IronXL 导出 CSV 的好处是什么? IronXL 提供了一种简单且高效的方式来处理 .NET Core 中的 CSV 导出。它支持多平台,不需要 Microsoft Office,并提供公式计算、数据排序和大数据集处理等功能。 如何在 .NET Core 项目中安装 IronXL? 要在 .NET Core 项目中安装 IronXL,您可以使用命令 Install-Package IronXL 的包管理器控制台,或在 Visual Studio 的 NuGet 包管理器 UI 中搜索并安装 'IronXL'。 我可以在 Windows 以外的平台上使用 IronXL 吗? 是的,IronXL 兼容多个平台,包括 Windows、macOS 和 Linux。它还支持 Xamarin、Azure、.NET Core 和 .NET Framework 应用程序,使其可以适用于各种环境。 您如何使用 IronXL 处理 Excel 中的大数据集? IronXL 可以通过利用其强大的 C# API 高效地处理 Excel 中的大数据集。它允许快速的数据操作,支持各种 Excel 格式,并提供数据排序和验证等功能。 IronXL 支持 Excel 公式计算吗? 是的,IronXL 支持 Excel 公式计算。您可以使用 IronXL 的 API 对电子表格数据进行复杂计算,类似于在 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# 中加载 Excel 文件C# CSV 库(开发者教程)
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多