如何在C#中使用IronXL读取CSV文件

Read a CSV File in C#

This article was translated from English: Does it need improvement?
Translated
View the article in English

When you need to read CSV files in C#, IronXL is an easy answer. You can read a CSV file with commas, or any other delimiter, as seen in the code segments below.

Quickstart: Load and convert a CSV file using IronXL in one line

This example shows how easy it is to read a CSV file using IronXL’s LoadCSV method and save it as an Excel workbook, letting you get started in seconds with minimal code.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    WorkBook wb = WorkBook.LoadCSV("data.csv", ExcelFileFormat.XLSX, listDelimiter: ","); wb.SaveAs("output.xlsx");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

Reading CSV Files in .NET Applications

  • Install a C# library for Reading CSV Files (IronXL)
  • Read CSV files in C#
  • Specify file format and delimiter
How To Work related to Read a CSV File in C#

Step 1

1. Install the IronXL Library

Before you can make use of IronXL to read CSV files in MVC or ASP or dotnet core, you need to install it first. Here is a quick walk-through.

  • In Visual Studio, select the Project menu
  • Manage NuGet Packages
  • Search for IronXL.Excel
  • Install
IronXL.Excel NuGet Package
Figure 1 - IronXL.Excel NuGet Package

Or Download from the Iron Software website, here: https://ironsoftware.com/csharp/excel/packages/IronXL.zip


How to Tutorial

2. Read CSV Files Programmatically

Now for the project!

Add the IronXL Namespace

// This namespace is required to access the IronXL functionalities
using IronXL;
// This namespace is required to access the IronXL functionalities
using IronXL;
' This namespace is required to access the IronXL functionalities
Imports IronXL
$vbLabelText   $csharpLabel

Add code to read a CSV file programmatically with IronXL and C#

:path=/static-assets/excel/content-code-examples/how-to/csharp-read-csv-read.cs
// Load the CSV file into a WorkBook object, specifying the file path, format, and delimiter
WorkBook workbook = WorkBook.LoadCSV("Read_CSV_Ex.csv", fileFormat: ExcelFileFormat.XLSX, listDelimiter: ",");

// Access the default worksheet within the loaded workbook
WorkSheet ws = workbook.DefaultWorkSheet;

// Save the workbook as an Excel file with a specified name
workbook.SaveAs("Csv_To_Excel.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
A CSV file opened in Notepad
Figure 2 - A CSV file opened in Notepad

A Workbook object is created. The LoadCSV method of the Workbook object is then used to specify the name of the CSV file to be read, the format to read it into, and what the delimiter is for the file. In this case, a comma is used as a separator.

A Worksheet object is then created. This is where the contents of the CSV file will be placed. Then the file is saved under a new name and format.

The CSV file opened in Excel
Figure 3 - The CSV file opened in Excel

Library Quick Access

IronXL API Reference Documentation

Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL API Reference Documentation.

IronXL API Reference Documentation
Documentation related to 2. Read CSV Files Programmatically

常见问题解答

如何在不使用 Interop 的情况下在 C# 中读取 CSV 文件?

您可以通过利用 IronXL 库在 C# 中读取 CSV 文件,而无需使用 Interop。首先,通过 Visual Studio 中的 NuGet 包管理器安装 IronXL。然后使用 WorkBook.LoadCSV 方法将 CSV 文件加载到 WorkBook 对象中。

使用 IronXL 在 C# 中读取 CSV 文件涉及哪些步骤?

要使用 IronXL 读取 CSV 文件,首先安装 IronXL 库。然后,创建一个新的 C# 项目,添加 using IronXL; 命名空间,并使用 WorkBook.LoadCSV 方法加载和操作 CSV 文件数据。

IronXL 可以处理具有不同分隔符的 CSV 文件吗?

是的,IronXL 允许您在读取 CSV 文件时指定不同的分隔符。您可以通过在 LoadCSV 方法选项中设置分隔符来调整它。

如何在 C# 中将 CSV 文件保存为 Excel 文件?

在使用 IronXL 将 CSV 文件加载到 WorkBook 对象后,您可以使用 SaveAs 方法将其保存为您所需格式的 Excel 文件,例如 XLSX。

为什么在 C# 中进行 CSV 操作时使用 IronXL 而不是 Microsoft Interop?

在 C# 中进行 CSV 操作时,IronXL 优于 Microsoft Interop,因为它不需要在服务器上安装 Excel,它提供了一种更高效和简单的方式来处理 C# 应用程序中的 CSV 文件。

如何在使用 IronXL 的 CSV 文件中指定要操作的工作表?

当您使用 IronXL 加载 CSV 文件时,它会存储在 WorkBook 对象中。您可以使用默认的 WorkSheet 对象来访问和操作数据,默认代表 CSV 文件的内容。

是否可以使用 IronXL 将 CSV 读取功能集成到 ASP.NET 应用程序中?

是的,通过安装库并利用其方法加载、读取和操作 CSV 数据,您可以将 CSV 读取功能集成到 ASP.NET 或 .NET Core 应用程序中。

我在哪里可以找到 IronXL API 参考文档?

在 Iron Software 网站上可以找到 IronXL API 参考文档。它提供了有关所有可用方法的详细信息,包括读取和操作 CSV 文件的方法。

LoadCSV 方法在 IronXL 中的目的是什么?

LoadCSV 方法在 IronXL 中用于将 CSV 数据导入到 WorkBook 对象中,使您能够像在 Excel 格式中一样操作和交互数据。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布