使用 IRONXL 如何在 C# 中导出文件到 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 This article will contrast and compare various methods that .NET technologies can interface programmatically with Microsoft Excel documents using one of the most well-known libraries, IronXL. Moreover, it will create a setting for writing, reading, and exporting Excel spreadsheets to CSV files. 1.1 What is .NET Framework? Microsoft created the proprietary .NET Framework, a software framework that is mostly compatible with Microsoft Windows. It served as the main Common Language Infrastructure (CLI) implementation until the cross-platform .NET project took its place. It offers language compatibility across several different programming languages and comes with a sizable class library called the Framework Class Library (FCL). 1.2 What is CSV? A text file format called "comma-separated values" divides values into separate columns using commas. Tabular data is stored in plain text in a CSV format file, where each line typically corresponds to a single data record. In the CSV file, each record has the same number of fields, which are separated by commas. 2.0 IronXL Library Features Microsoft Excel documents can be read and converted to CSV files using the IronXL for .NET C# Excel library. Without installing Microsoft Office or Microsoft.Office.Interop.Excel, users can utilize IronXL, a stand-alone .NET software library. It is capable of reading several spreadsheet formats. IronXL's simple C# API makes it easy to read, edit, and produce Excel spreadsheets in a .NET context. IronXL provides complete support for Xamarin, Linux, macOS, Azure, .NET Core, and .NET Framework. IronXL, a C# library compatible with both .NET Core and .NET Framework, is among the best for Excel spreadsheets. IronXL supports almost every .NET Framework, including Web apps, Windows Forms, and Console. IronXL can be used with Linux, macOS, and Windows operating systems. IronXL has quick and easy access to Excel files. IronXL is capable of reading a wide range of Excel file types, including XLSX, CSV, XLS, XLST, TSV, XLSM, and more. The functions for importing, updating, and exporting data tables and datasets are only a few of the library's numerous options. For the Excel spreadsheet, IronXL is capable of producing calculations. IronXL supports numerous data types for Excel columns, including text, integers, dates, currencies, formulae, and percentages. IronXL can handle multiple values in the form of dates, currencies, percentages, text, numbers, formulas, and more Excel column data types. To know how to export data into Excel refer to this tutorial. 3.0 Creating a New Project in Visual Studio Open Visual Studio and create a .NET project before using the IronXL library. Visual Studio can be used in any version, though the most recent version is recommended. To keep things simple, this article will be using the Console Application for this example. Open Visual Studio, go to "File" menu and select "New Project". From the various .NET project templates choose the "Console App". Enter the project's location and name after that. Create a new project in Visual Studio The Framework drop-down menu can be used to choose a .NET Framework. .NET Framework 4.7 is selected for this project. Next, click on the Create button. The program.cs file will open when the application generates the solution, allowing you to enter the code and build/run the program. The Program.cs file will open once the Console Application project is successfully created. To test the code, the next step is to add the IronXL library. Install the IronXL library, since it is required for the next fix. To do this, enter the following command into the NuGet Package Manager Console: Install-Package IronXL -Version 2023.8.29 Install the IronXL package in the NuGet Package Manager Console Another option is to use the NuGet Package Manager to look for the package "IronXL", then choose the necessary package to download from this list of all the NuGet packages connected to IronXL. Install the IronXL package using the NuGet Package Manager 4.0 Export to CSV file IronXL makes it simple and quick to create string arrays to CSV files. Writing CSV files is made simpler as a result. Firstly, include the IronXL namespace, as shown in the code screenshot below. Then, use IronXL's classes and methods in the below code once it has been presented. Include the IronXL namespace Excel files can be created with IronXL and then turned into workbook objects. This object offers several methods for working with them. The following example of code generates an Excel file by converting an array string into an Excel worksheet. using IronXL; using IronXL.Options; using System; class Program { static void Main(string[] args) { // Array of student names string[] students = { "AAA", "BBB", "CCC", "DDD", "EEE", "FFF" }; // Create a new workbook with an Excel file format of XLS var workBook = WorkBook.Create(ExcelFileFormat.XLS); // Get the default worksheet (where student data will be written) var workSheet = workBook.DefaultWorkSheet; // Start adding data to worksheet from row 1 int rowCount = 1; foreach (var student in students) { // Set the value in column A for student ID workSheet["A" + rowCount].Value = rowCount.ToString(); // Set the value in column B for student name workSheet["B" + rowCount].Value = student.ToString(); rowCount++; } // Save the workbook as a CSV file with a specified delimiter workBook.SaveAsCsv("Sample.csv", ";"); // Alternately, convert the workbook to a stream var stream = workBook.ToStream(); } } using IronXL; using IronXL.Options; using System; class Program { static void Main(string[] args) { // Array of student names string[] students = { "AAA", "BBB", "CCC", "DDD", "EEE", "FFF" }; // Create a new workbook with an Excel file format of XLS var workBook = WorkBook.Create(ExcelFileFormat.XLS); // Get the default worksheet (where student data will be written) var workSheet = workBook.DefaultWorkSheet; // Start adding data to worksheet from row 1 int rowCount = 1; foreach (var student in students) { // Set the value in column A for student ID workSheet["A" + rowCount].Value = rowCount.ToString(); // Set the value in column B for student name workSheet["B" + rowCount].Value = student.ToString(); rowCount++; } // Save the workbook as a CSV file with a specified delimiter workBook.SaveAsCsv("Sample.csv", ";"); // Alternately, convert the workbook to a stream var stream = workBook.ToStream(); } } Imports IronXL Imports IronXL.Options Imports System Friend Class Program Shared Sub Main(ByVal args() As String) ' Array of student names Dim students() As String = { "AAA", "BBB", "CCC", "DDD", "EEE", "FFF" } ' Create a new workbook with an Excel file format of XLS Dim workBook = WorkBook.Create(ExcelFileFormat.XLS) ' Get the default worksheet (where student data will be written) Dim workSheet = workBook.DefaultWorkSheet ' Start adding data to worksheet from row 1 Dim rowCount As Integer = 1 For Each student In students ' Set the value in column A for student ID workSheet("A" & rowCount).Value = rowCount.ToString() ' Set the value in column B for student name workSheet("B" & rowCount).Value = student.ToString() rowCount += 1 Next student ' Save the workbook as a CSV file with a specified delimiter workBook.SaveAsCsv("Sample.csv", ";") ' Alternately, convert the workbook to a stream Dim stream = workBook.ToStream() End Sub End Class $vbLabelText $csharpLabel The aforementioned code example will export an array to an Excel file. Column heads are created once an array has been constructed. The rows are added one at a time when the first column is established. The WorkBook object is created once the data has been added to the array string. You can add data to an Excel sheet using the WorkBook object, and then save it somewhere else. The goal is to create worksheets by creating the WorkSheet object, which can be linked to the workbook object. Before adding each item to the spreadsheet, a foreach loop is used to read each item from the array string. The data is saved as a CSV file using the SaveAsCsv method when all the data has been entered into the worksheet. Along with the delimiter, the file name and location can also be provided as optional parameters. The library then assists in writing data to a new CSV file. If you want to read CSV files instead of using Microsoft Excel, you can use Notepad. IronXL also supports storing data in multiple file formats, such as XLS, CSV, and XLSX, by using the Save method. Or the workbook can be turned into a stream and then write data into the required location. OUTPUT CSV File The output Excel file The output of the code sample that was run is seen above. The newly produced Excel sheet in the screenshot has each item of data from the string array added independently. To know more about how to export data into CSV using IronXL, please refer to this step-by-step tutorial. 5.0 Conclusion One of the most popular Excel add-ons is IronXL. It doesn't rely on any other outside libraries. It is self-contained and does not require the installation of Microsoft Excel. It operates via several channels. IronXL provides a complete solution for all programmatically executed Microsoft Excel document-related tasks. It is possible to perform calculations, sort strings or numbers, trim, add, locate and replace, merge and unmerge, save files, and more. You can define cell data types in addition to checking spreadsheet data. It lets you read and write files and makes dealing with Excel data easier. IronXL offers a free trial license, which gives users a chance to try all its key features for free. IronXL is available for $799 at launch. If users would like updates and help with the software, they can also choose to pay a one-year subscription charge. For an additional fee, IronXL offers protection for unlimited redistribution. To find more accurate pricing information, please visit IronXL's license page. 常见问题解答 我如何在 C# 中以编程方式将 Excel 数据导出为 CSV? 要在 C# 中以编程方式将 Excel 数据导出为 CSV,可以使用 IronXL。首先,在您的 C# 程序中创建工作簿和工作表,用数据填充它,然后使用 SaveAsCsv 方法将数据导出为 CSV 文件。 使用 C# 导出为 CSV 是否需要安装 Microsoft Excel? 不,不需要安装 Microsoft Excel。IronXL 是一个独立的 .NET 库,允许您读取、编辑和导出 Excel 文件为 CSV,而不需要 Microsoft Office 或 Microsoft.Office.Interop.Excel。 如何在 Visual Studio 项目中安装 IronXL? 您可以通过使用 NuGet 包管理器控制台来在 Visual Studio 项目中安装 IronXL。运行命令 Install-Package IronXL -Version 2023.8.29 以将 IronXL 添加到您的项目。 使用 IronXL 处理 C# 中的 Excel 任务有哪些好处? IronXL 提供了用于处理 C# 中 Excel 任务的强大功能集,如读取、编辑和导出 Excel 电子表格为 CSV 文件。它支持多种 .NET 框架和操作系统,使其能适应不同环境而无需外部库。 IronXL 能处理 Excel 文件中的计算和数据操作吗? 是的,IronXL 可以处理 Excel 文件中的计算、排序、合并以及不同数据类型的管理,为与 Excel 相关的任务提供全面的解决方案。 IronXL 支持哪些平台进行 Excel 集成? IronXL 支持多个平台,包括 .NET Core、.NET Framework、Xamarin、Linux、macOS、Azure 和 Windows,允许在不同系统上灵活部署。 IronXL 可以读取和写入哪些文件格式? IronXL 可以读取和写入各种 Excel 文件格式,包括 XLSX、CSV、XLS、XLST、TSV、XLSM 等,便于无缝的数据集成和导出。 IronXL 有试用版吗? 是的,IronXL 提供了一个免费试用版,允许您在购买完整许可证前探索其功能和能力。 如何使用 C# 将数据数组转换为 CSV 文件? 使用 IronXL,您可以将数据数组转换为 Excel 工作表,然后使用 SaveAsCsv 方法将其导出为 CSV 文件,使数据转换直接而高效。 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# 中编辑电子表格
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多