使用 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 explore how to create a C# CSV Reader using the IronXL library. IronXL - Excel Library IronXL is a powerful Excel library that provides C# developers with the ability to create, load, read, and edit Excel spreadsheets in various formats. Designed specifically for .NET, IronXL prioritizes speed, accuracy, and ease of use. It allows for saving Excel files in different formats and loading various spreadsheet formats into Excel for efficient data reading. IronXL supports Excel workbook formats with different file extensions, including CSV and TSV, XLS and XLSX, XSLT and XLSM. It is compatible with the latest version of the .NET Framework, as well as all previous versions up to 2.0. IronXL can be used on various platforms, including Linux, MacOS, Azure, Docker, and AWS. How to make a CSV Reader in C# using IronXL? Prerequisites To convert data from a CSV file to an Excel file and read it in C#, we require the following tools: Visual Studio: is an essential Integrated Development Environment (IDE) for working on C# .NET applications. It is the official and recommended IDE for developing C# apps, and it can be downloaded and installed from the Microsoft website. However, other IDEs that support a C# environment can also be used if preferred by the developer. Create Console App: Follow the steps to create a simple Console Application. Open Visual Studio and click "Create a Project". New Project Window Choose "Console App" from the list of options available. Make sure the selected language is C#. Create a new Console App Next, name your project whatever you want. Configuration The next step in project configuration is selecting the .NET Framework. The latest version available is .NET 7.0, which is supported in the standard term. However, to avoid any potential errors, you can opt for the more stable version of .NET 6.0, as Microsoft provides long-term support for it. It's important to choose the appropriate version based on your specific requirements. IronXL - It is the .NET Excel library that is designed to work with Excel spreadsheets. It must be installed in your C# application before using it. You can download and install the package from multiple sources: NuGet - the package manager for .NET. You can directly download IronXL from the NuGet website. Visual Studio NuGet Package Manager: Open the Package Manager by going to Tools > NuGet Package Manager > Manage Packages for Solution... Download the IronXL .NET Excel DLL zip file directly from the Iron website and add it to the Project Solution as a project reference. Add IronXL Namespace Once you have the prerequisites, the next step is to add the IronXL namespace on top of the source code in the main.cs file: using IronXL; using IronXL; Imports IronXL $vbLabelText $csharpLabel Open an Existing CSV File IronXL provides an easy way to read CSV files in C#. First, open a CSV file for reading. It is a file type which is based on rows and columns. Here, the WorkBook class is used along with its LoadCSV method to open a CSV file. The code is as follows: // Load the CSV file and convert it to XLSX format. var csv = WorkBook.LoadCSV("color_srgb.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ","); // Load the CSV file and convert it to XLSX format. var csv = WorkBook.LoadCSV("color_srgb.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ","); ' Load the CSV file and convert it to XLSX format. Dim csv = WorkBook.LoadCSV("color_srgb.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",") $vbLabelText $csharpLabel The LoadCSV method in IronXL allows you to open a CSV file and convert it to XLSX format. This method provides options to specify the list delimiter used in the CSV file. In this example, the default delimiter, which is a comma, is used. The resulting file is opened and can be further processed using IronXL's features for reading and manipulating Excel spreadsheets. CSV File Get Worksheet from CSV Reader Class In the previous step, the CSV file was opened as an Excel workbook using IronXL. Now, get the default worksheet from the workbook using the WorkSheet class. The following example demonstrates how to get the worksheet for reading CSV data: // Retrieve the default worksheet from the loaded workbook. WorkSheet ws = csv.DefaultWorkSheet; // Retrieve the default worksheet from the loaded workbook. WorkSheet ws = csv.DefaultWorkSheet; ' Retrieve the default worksheet from the loaded workbook. Dim ws As WorkSheet = csv.DefaultWorkSheet $vbLabelText $csharpLabel For more information on working with Excel worksheets, you can visit this code examples page. Read CSV Data using C# DataTable Once the CSV is loaded successfully and the data is available as a worksheet, the data can be read from the CSV file very easily in a C# DataTable. Convert Worksheet to DataTable First, create a DataTable instance and convert the worksheet data to a table using the ToDataTable method. The following code helps to achieve this task: // Convert the worksheet data to a DataTable. DataTable dt = ws.ToDataTable(true); // Convert the worksheet data to a DataTable. DataTable dt = ws.ToDataTable(true); ' Convert the worksheet data to a DataTable. Dim dt As DataTable = ws.ToDataTable(True) $vbLabelText $csharpLabel Start Reading CSV Data Now, iterate through all the records using the DataTable instance. The data is received in rows and columns. First, then move through each column to get its value. To get all the records along with the header row, use the following code snippet: // Iterate over each row in the DataTable. foreach (DataRow row in dt.Rows) { // Iterate over each column in the current row. for (int i = 0; i < dt.Columns.Count; i++) { // Print each column value in the current row. Console.Write(row[i] + " "); } // Move to the next line after printing all columns of the current row. Console.WriteLine(); } // Iterate over each row in the DataTable. foreach (DataRow row in dt.Rows) { // Iterate over each column in the current row. for (int i = 0; i < dt.Columns.Count; i++) { // Print each column value in the current row. Console.Write(row[i] + " "); } // Move to the next line after printing all columns of the current row. Console.WriteLine(); } ' Iterate over each row in the DataTable. For Each row As DataRow In dt.Rows ' Iterate over each column in the current row. For i As Integer = 0 To dt.Columns.Count - 1 ' Print each column value in the current row. Console.Write(row(i) & " ") Next i ' Move to the next line after printing all columns of the current row. Console.WriteLine() Next row $vbLabelText $csharpLabel In the above code, a foreach loop is used to get a single record from a collection of rows. Then inside a nested for loop, the number of columns is counted and finally, the data from each row is printed on the screen. The output is formatted similarly to a CSV file. Output Summary This article demonstrated how to create a CSV reader in C# using the IronXL library. Loading CSV files is made easy with IronXL as it supports the CSV format in Excel spreadsheets. The DataTable object is used to create an elegant CSV reader and format the output to match the original file. IronXL also provides convenient conversion between different file formats and allows for creating Excel files from scratch without requiring Interop and Microsoft Excel to be installed. It is also compatible with C# DataSet and DataTable, providing developers with flexibility in interconverting data without relying on third-party applications. With its powerful features, IronXL is a valuable tool for C# developers to work with Excel spreadsheets and handle CSV data efficiently. IronXL is free for development. However, for commercial use, you need to purchase its license starting from $799. You can also use its free trial to test its compiled .dll compatibility with your project in production mode. 常见问题解答 如何在 C# 中创建不使用 Interop 的 CSV 读取器? 您可以通过利用 IronXL 库在 C# 中创建不使用 Interop 的 CSV 读取器。首先,通过 NuGet 安装 IronXL 或从 IronXL 网站下载 .NET Excel DLL。在您的代码中添加 IronXL 命名空间,并使用 WorkBook.LoadCSV 方法加载 CSV 文件,使您可以将其作为 Excel 工作簿进行操作。 使用 IronXL 进行 CSV 操作的好处是什么? IronXL 为在 C# 中处理 CSV 操作提供了强大而灵活的解决方案。它允许开发者加载、读取并将 CSV 文件转换为 Excel 格式,而无需 Microsoft Excel 或 Interop,从而使其兼容于 Linux、MacOS 和 Azure、AWS 等各种平台。 如何使用 IronXL 将 CSV 数据转换为 Excel 格式? 要使用 IronXL 将 CSV 数据转换为 Excel 格式,使用 WorkBook.LoadCSV 方法加载 CSV 文件,然后使用 WorkBook.SaveAs 方法保存,指定所需的 Excel 格式,如 XLSX。 在 C# 中使用 IronXL 将 CSV 数据读入 DataTable 是否可行? 是的,您可以在 C# 中使用 IronXL 将 CSV 数据读入 DataTable。将 CSV 作为工作簿加载,访问默认工作表,并使用 Range.ToDataTable 方法将工作表数据转换为 DataTable 以便于操作和迭代。 IronXL 库兼容哪些平台? IronXL 库兼容多个平台,包括 Windows、Linux、MacOS、Azure、Docker 和 AWS。此跨平台支持使其成为在多样化环境中工作的开发者的一个多才多艺的选择。 IronXL 可以用于商业项目吗? 是的,IronXL 可以用于商业项目。虽然它提供了用于开发和测试的免费试用,生产使用需要商业许可证。此许可证提供对全方位功能和支持的访问。 IronXL 如何在 C# 应用程序中增强 CSV 数据操作? IronXL 通过允许开发者加载和编辑 CSV 文件而无需依赖 Microsoft Excel,从而在 C# 应用程序中增强 CSV 数据操作。它提供在 CSV 和 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# 中将数据集转换为 Excel如何从 Excel 文件中获取单...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多