使用 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 Have you ever been stuck trying to parse CSV files and convert them to XLSX or XLSX files to CSV files for some important task in .NET, but couldn't figure out how to do so without writing tons of code? Many CSV libraries exist to solve this issue. However, the IronXL C# Excel Library will be used in this blog to perform these tasks with just two lines of code. To get started, all you need is Visual Studio, and follow the instructions to install in detail below. 1. Creating a New Project in Visual Studio Open the Visual Studio editor. Go to the File menu in Visual Studio. Choose "New Project", then select Console Application. Type the project name and choose the project location. Then, click the Create button to create the project. Select the required .NET Framework, as shown in the screenshot below: Create a new C#. NET Console Application The program.cs file will open so you can enter the logic and create/run the application. 2. Install the IronXL C# Library The IronXL library can be downloaded and installed in many different ways. Today, we will focus on two of these: Using the Visual Studio NuGet Package Manager Using the Visual Studio Command-Line 2.1. Using the Visual Studio NuGet Package Manager The NuGet Package Manager UI is available in Visual Studio to install the package directly into the project. The below screenshot shows how to open it. Installing IronXL using Visual Studio NuGet Package Manager GUI The Package Manager UI provides a Browse feature that displays a list of the package libraries that are offered on the NuGet website. Enter the "IronXL" keyword, as in the below screenshot, to find the IronXL package. Locate the IronXL library in NuGet Package Manager by searching for it in the Browse section Select the IronXL.Excel package and click the Install button to add it to the project. 2.2. Using the Visual Studio Command-Line In the Visual Studio menu, go to Tools > NuGet Package Manager > click on Package Manager Console. Access the NuGet Package Manager Console within Visual Studio from the Tools menu The Package Manager Console will appear at the bottom of the screen. Just type the following command then press enter, and IronXL will be installed. Install-Package IronXL.Excel Install the IronXL library through Command-Line 3. Parsing CSV Files Parsing CSVs manually requires writing a lot of precise code to get the job done, but with IronXL, it requires just a few lines of code. By using only conventional C# code to parse CSV formatted files, you will have to use a lot of bulky code. Here is an example of code to achieve this. using FileHelpers; using System; namespace parse_csv { [DelimitedRecord(",")] public class Record { public string Name; public string Age; } class Program { static void Main(string[] args) { // Create a FileHelperEngine for the Record class var fileHelperEngine = new FileHelperEngine<Record>(); // Read records from the CSV file into an array var records = fileHelperEngine.ReadFile(@"C:\File\records.csv"); // Print each record's Name and Age foreach (var record in records) { Console.WriteLine(record.Name); Console.WriteLine(record.Age); } } } } using FileHelpers; using System; namespace parse_csv { [DelimitedRecord(",")] public class Record { public string Name; public string Age; } class Program { static void Main(string[] args) { // Create a FileHelperEngine for the Record class var fileHelperEngine = new FileHelperEngine<Record>(); // Read records from the CSV file into an array var records = fileHelperEngine.ReadFile(@"C:\File\records.csv"); // Print each record's Name and Age foreach (var record in records) { Console.WriteLine(record.Name); Console.WriteLine(record.Age); } } } } Imports FileHelpers Imports System Namespace parse_csv <DelimitedRecord(",")> Public Class Record Public Name As String Public Age As String End Class Friend Class Program Shared Sub Main(ByVal args() As String) ' Create a FileHelperEngine for the Record class Dim fileHelperEngine As New FileHelperEngine(Of Record)() ' Read records from the CSV file into an array Dim records = fileHelperEngine.ReadFile("C:\File\records.csv") ' Print each record's Name and Age For Each record In records Console.WriteLine(record.Name) Console.WriteLine(record.Age) Next record End Sub End Class End Namespace $vbLabelText $csharpLabel But using IronXL this can be achieved in just a few lines of code. Using IronXL you can parse CSV files from XLSX, XLS, TSV, and more. In this tutorial, we'll explore the following conversions: Parse CSV file from XLSX file Parse CSV file From XLS file Parse CSV file From TSV file 3.1. Parse a CSV File from an XLSX File Open Microsoft Excel and create a new XLSX file. Populate its rows and columns with some mock data. The below image shows the file used for all conversions in this tutorial. Sample Excel data Once you have your file ready, write the following example code and execute the program. using IronXL; class Program { static void Main() { // Load the XLSX file into a WorkBook object WorkBook wb = WorkBook.Load("test.xlsx"); // Save the WorkBook as a CSV file wb.SaveAsCsv("Parsed CSV.csv"); } } using IronXL; class Program { static void Main() { // Load the XLSX file into a WorkBook object WorkBook wb = WorkBook.Load("test.xlsx"); // Save the WorkBook as a CSV file wb.SaveAsCsv("Parsed CSV.csv"); } } Imports IronXL Friend Class Program Shared Sub Main() ' Load the XLSX file into a WorkBook object Dim wb As WorkBook = WorkBook.Load("test.xlsx") ' Save the WorkBook as a CSV file wb.SaveAsCsv("Parsed CSV.csv") End Sub End Class $vbLabelText $csharpLabel After the execution is completed a new file named Parsed CSV.csv will be created. Reading CSV files can be done in any editor or reader you like. The below image shows the output of the above command - our generated CSV data. In the output file, double quotation marks represent bold values. The result of invoking WorkBook.SaveAsCsv method on the sample Excel Workbook 3.2. Parse CSV File from XLS File In this example, we will see how to convert XLS files into CSV format. First, let's create an example XLS file that we can convert to CSV format. A sample XLS file Next, we will execute the block of code below to convert the sample XLS file into a CSV file. using IronXL; class Program { static void Main() { // Load the XLS file into a WorkBook object WorkBook wb = WorkBook.Load("XLS.xls"); // Save the WorkBook as a CSV file wb.SaveAsCsv("Example2.csv"); } } using IronXL; class Program { static void Main() { // Load the XLS file into a WorkBook object WorkBook wb = WorkBook.Load("XLS.xls"); // Save the WorkBook as a CSV file wb.SaveAsCsv("Example2.csv"); } } Imports IronXL Friend Class Program Shared Sub Main() ' Load the XLS file into a WorkBook object Dim wb As WorkBook = WorkBook.Load("XLS.xls") ' Save the WorkBook as a CSV file wb.SaveAsCsv("Example2.csv") End Sub End Class $vbLabelText $csharpLabel After the execution of the above code is finished, you will have a newly generated CSV file. The result CSV file from saving using the code above 3.3. Parse CSV file From TSV file Spreadsheet applications frequently employ TSV files, or Tab-Separated Values files, to transfer data across databases. It saves a data table with tabs separating the data columns and each record being on a different line. IronXL offers a CSV parser to parse CSV files from TSV format for better data management. Let's get started with the example. A sample TSV spreadsheet using IronXL; class Program { static void Main() { // Load the TSV file into a WorkBook object WorkBook wb = WorkBook.Load("TSV.tsv"); // Save the WorkBook as a CSV file wb.SaveAsCsv("Example3.csv"); } } using IronXL; class Program { static void Main() { // Load the TSV file into a WorkBook object WorkBook wb = WorkBook.Load("TSV.tsv"); // Save the WorkBook as a CSV file wb.SaveAsCsv("Example3.csv"); } } Imports IronXL Friend Class Program Shared Sub Main() ' Load the TSV file into a WorkBook object Dim wb As WorkBook = WorkBook.Load("TSV.tsv") ' Save the WorkBook as a CSV file wb.SaveAsCsv("Example3.csv") End Sub End Class $vbLabelText $csharpLabel Below is the output in CSV format. The output CSV file 4. Conclusion This tutorial uses IronXL to parse varying file formats to CSV in C#. Additionally, the IronXL library also provides the following features: A broad set of functions, including data manipulation, and data export. Support chart management that is fully compatible with Excel. Support for cell formatting such as text alignment, font size, color, etc. Ability to customize any methods available in Microsoft Excel including freeze panel, adding formulas, and applying conditional formatting. Compatible with Excel encryption with a password. Check out IronXL's features, Code Examples and documentation content for more information about how IronXL works. Download IronXL and try out free for 30 Days with a trial license key. Visit the Licensing Page for more information about licensing terms and conditions. Purchase the complete Iron Suite to retrieve licenses for all five Iron Software libraries for the price of two IronXL library licenses! Thanks for reading! 常见问题解答 如何在 C# 中不使用 Interop 解析 CSV 文件? IronXL 允许您在 C# 中解析 CSV 文件,而无需使用 Interop。您可以将 CSV 文件加载到 WorkBook 对象中并直接操作,将其转换为其他格式如 XLSX 或 XLS,只需几行代码即可。 在 Visual Studio 中安装 IronXL 库需要哪些步骤? 要在 Visual Studio 中安装 IronXL 库,请打开 NuGet 包管理器 UI,搜索 'IronXL' 并安装。或者,您可以使用 Visual Studio 命令行并在包管理器控制台中运行命令 Install-Package IronXL.Excel。 如何在 C# 中将 CSV 文件转换为 Excel 格式? 使用 IronXL,您可以通过将 CSV 加载到 WorkBook 对象中,然后使用 SaveAsXlsx 方法以所需格式保存,将 CSV 文件转换为像 XLSX 或 XLS 的 Excel 格式。 是否可以在 C# 中解析 TSV 文件并将其转换为 CSV? 是的,IronXL 允许解析 TSV 文件。将 TSV 文件加载到 WorkBook 对象中,然后用 SaveAsCsv 方法将其转换为 CSV 文件。 C# 的 Excel 库提供哪些功能来进行数据操作? IronXL 提供诸如数据操作、图表管理、单元格格式化和兼容 Excel 加密等功能。它支持冻结窗格、公式和条件格式设置等操作。 如何在 C# 中编程管理 Excel 电子表格格式? IronXL 使能够管理如 XLSX、XLS 和 CSV 等不同电子表格格式。它提供了在这些格式之间转换并在 .NET 应用程序中高效处理数据的方法。 在购买之前,我可以尝试 C# 的 Excel 库吗? 是的,IronXL 提供 30 天免费试用,可以从 NuGet 网站下载。这可以让您测试其功能,并确保满足您的需求后再购买。 IronXL 在解析和转换 CSV 文件方面有哪些优势? IronXL 通过最少代码简化了 CSV 文件的解析和转换过程。它确保了高效的数据处理,并提供了增强 C# 应用程序中 Excel 文件操作的广泛功能。 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# 中创建 CSV 文件(逐步教程)如何在 Blazor 中使用 IronXL ...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多