跳至页脚内容
使用 IRONXL

如何在 C# 中将 CSV 文件读取到 DataTable

Working with CSV files is a common task for .NET developers., whether you're importing sales reports, processing inventory, or syncing customer records. Converting a CSV file into a DataTable makes it easy to manipulate, analyze, or insert into a database table.

Reading CSV files in C# can be tricky, large files, different delimiters, or embedded commas often require complex parsing or Excel. IronXL simplifies this: with just a few lines of code, you can read CSV files from any file path, convert them into a DataTable with proper column headers, and prepare them for bulk database operations - all without Excel.

In this guide, you’ll learn how to:

  • Load a CSV file into a DataTable in C#
  • Handle different delimiters like commas, tabs, or semicolons
  • Import your DataTable directly into SQL Server efficiently
  • Manage large datasets reliably without running into memory issues

By the end, you’ll have a complete, practical workflow for turning CSV data into actionable information using IronXL in your .NET applications.

Why Is CSV to DataTable Conversion Essential?

CSV to DataTable conversion allows developers to transform comma separated values (CSV data) into structured objects like a DataTable for further processing. Whether you're handling inventory data, customer records, or transaction logs, being able to convert CSV to a new DataTable efficiently is crucial. Using the first row as column headers, you can ensure that datatable columns align with your database table schema.

Traditional approaches often struggle with large files, different delimiters, or memory management. IronXL eliminates these challenges and handles different delimiters, quoted fields, and embedded commas seamlessly. IronXL's CSV reading capabilities eliminate these common pain points.

How to Install IronXL?

Getting started with IronXL requires just a simple NuGet package installation. Open the NuGet Package Manager Console in Visual Studio and execute:

Install-Package IronXL.Excel

Once installed, add the IronXL namespace to your project:

using IronXL;
using System.Data;
using IronXL;
using System.Data;
Imports IronXL
Imports System.Data
$vbLabelText   $csharpLabel

This provides access to all the powerful CSV processing capabilities without any Excel dependencies. For more details on IronXL's extensive API, refer to the API Reference Documentation.

NuGet 使用 NuGet 安装

PM >  Install-Package IronXL.Excel

IronXL 上查看 NuGet 快速安装。超过 1000 万次下载,它正以 C# 改变 PDF 开发。 您也可以下载 DLL

How to Convert CSV to DataTable?

The beauty of IronXL lies in its simplicity. Here's how to read a CSV file and convert it to a DataTable:

// Load CSV file into a WorkBook object
WorkBook workbook = WorkBook.LoadCSV("sales_data.csv", 
    fileFormat: ExcelFileFormat.XLSX);
// Access the default worksheet
WorkSheet worksheet = workbook.DefaultWorkSheet;
// Convert to DataTable with headers
DataTable dataTable = worksheet.ToDataTable(true);
// Display the data
foreach (DataRow row in dataTable.Rows)
{
    foreach (var item in row.ItemArray)
    {
        Console.Write($"{item}\t");
    }
    Console.WriteLine();
}
// Load CSV file into a WorkBook object
WorkBook workbook = WorkBook.LoadCSV("sales_data.csv", 
    fileFormat: ExcelFileFormat.XLSX);
// Access the default worksheet
WorkSheet worksheet = workbook.DefaultWorkSheet;
// Convert to DataTable with headers
DataTable dataTable = worksheet.ToDataTable(true);
// Display the data
foreach (DataRow row in dataTable.Rows)
{
    foreach (var item in row.ItemArray)
    {
        Console.Write($"{item}\t");
    }
    Console.WriteLine();
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The LoadCSV method intelligently parses your CSV file, automatically handling common cases such as quoted fields and embedded commas. The fileFormat parameter tells IronXL how to interpret the CSV data internally, ensuring it is treated as an Excel-compatible structure. Once loaded, the ToDataTable(true) method converts the worksheet into a DataTable, with the parameter `true` indicating that the first row should be used as column headers. This results in a clean, structured DataTable with named columns that match your CSV headers. For more advanced manipulation options, explore the complete Excel to DataTable tutorial.

How to Import DataTable into SQL Server?

Once you have your Data Table, importing it into SQL Server becomes remarkably efficient using SqlBulkCopy:

using System.Data;
using Microsoft.Data.SqlClient;
using IronXL;
class Program
{
    static void Main(string[] args)
    {
        // Create connection string
        string connectionString = @"Data Source=localhost;Initial Catalog=SalesDB;
                           Integrated Security=True;TrustServerCertificate=True;";
        // Read CSV into DataTable
        WorkBook workbook = WorkBook.LoadCSV("inventory_report.csv");
        DataTable dataTable = workbook.DefaultWorkSheet.ToDataTable(true);
        // Bulk insert into SQL Server
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
            {
                // Set destination table name
                bulkCopy.DestinationTableName = "dbo.Inventory";
                // Map DataTable columns to SQL table columns
                bulkCopy.ColumnMappings.Add("ProductCode", "product_code");
                bulkCopy.ColumnMappings.Add("Quantity", "quantity");
                bulkCopy.ColumnMappings.Add("LastUpdated", "last_updated");
                // Set batch size for better performance
                bulkCopy.BatchSize = 1000;
                // Write data to SQL Server
                bulkCopy.WriteToServer(dataTable);
            }
        }
        Console.WriteLine($"Successfully imported {dataTable.Rows.Count} records");
    }
}
using System.Data;
using Microsoft.Data.SqlClient;
using IronXL;
class Program
{
    static void Main(string[] args)
    {
        // Create connection string
        string connectionString = @"Data Source=localhost;Initial Catalog=SalesDB;
                           Integrated Security=True;TrustServerCertificate=True;";
        // Read CSV into DataTable
        WorkBook workbook = WorkBook.LoadCSV("inventory_report.csv");
        DataTable dataTable = workbook.DefaultWorkSheet.ToDataTable(true);
        // Bulk insert into SQL Server
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
            {
                // Set destination table name
                bulkCopy.DestinationTableName = "dbo.Inventory";
                // Map DataTable columns to SQL table columns
                bulkCopy.ColumnMappings.Add("ProductCode", "product_code");
                bulkCopy.ColumnMappings.Add("Quantity", "quantity");
                bulkCopy.ColumnMappings.Add("LastUpdated", "last_updated");
                // Set batch size for better performance
                bulkCopy.BatchSize = 1000;
                // Write data to SQL Server
                bulkCopy.WriteToServer(dataTable);
            }
        }
        Console.WriteLine($"Successfully imported {dataTable.Rows.Count} records");
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The SqlBulkCopy class provides exceptional performance for large-scale data imports. The ColumnMappings collection allows you to map DataTable columns to database columns with different names, ensuring flexibility in your data schema. The BatchSize property optimizes memory usage by processing records in chunks rather than loading everything at once. This approach can efficiently handle millions of records efficiently, making it ideal for enterprise-scale data operations. Learn more about IronXL's DataSet and DataTable capabilities.

Output

Here, we can use the query *SELECT FROM dbo.Inventory;** in SSMS to check that our CSV data actually got imported into the SQL Server. First, let's take a look at the data we used in our example CSV file:

How to Read a CSV File into a DataTable in C#: Figure 2 - Sample CSV data

Now, if you look at the output for our query, we can see it has successfully copied every column, header, and row into the database table.

How to Read a CSV File into a DataTable in C#: Figure 3 - Database view showing the imported records

How to Handle Different Delimiters?

Not all CSV files use commas. IronXL seamlessly handles various delimiters:

using IronXL;
using System;
using System.Data;
using System.IO;
class Program
{
    static void Main()
    {
        // --- Tab-delimited file ---
        string tsvPath = @"export_data.tsv";
        WorkBook tsvWorkbook = WorkBook.LoadCSV(tsvPath, ExcelFileFormat.XLSX, "\t");
        // --- Semicolon-delimited file ---
        string semiPath = @"european_data.csv";
        string tempCsv = Path.Combine(Path.GetTempPath(), "european_data_comma.csv");
        // Replace semicolons with commas for proper parsing
        var lines = File.ReadAllLines(semiPath);
        for (int i = 0; i < lines.Length; i++)
        {
            lines[i] = lines[i].Replace(';', ',');
        }
        File.WriteAllLines(tempCsv, lines);
        WorkBook semiWorkbook = WorkBook.LoadCSV(tempCsv, ExcelFileFormat.XLSX);
        // Print both workbooks
        PrintWorkbook(tsvWorkbook, "Tab-delimited File");
        PrintWorkbook(semiWorkbook, "Semicolon-delimited File");
    }
    static void PrintWorkbook(WorkBook workbook, string name)
    {
        Console.WriteLine($"\n--- {name} ---\n");
        DataTable dt = workbook.DefaultWorkSheet.ToDataTable(true);
        // Print column headers
        foreach (DataColumn col in dt.Columns)
        {
            Console.Write($"{col.ColumnName}\t");
        }
        Console.WriteLine();
        // Print rows
        foreach (DataRow row in dt.Rows)
        {
            foreach (var item in row.ItemArray)
            {
                Console.Write($"{item}\t");
            }
            Console.WriteLine();
        }
    }
}
using IronXL;
using System;
using System.Data;
using System.IO;
class Program
{
    static void Main()
    {
        // --- Tab-delimited file ---
        string tsvPath = @"export_data.tsv";
        WorkBook tsvWorkbook = WorkBook.LoadCSV(tsvPath, ExcelFileFormat.XLSX, "\t");
        // --- Semicolon-delimited file ---
        string semiPath = @"european_data.csv";
        string tempCsv = Path.Combine(Path.GetTempPath(), "european_data_comma.csv");
        // Replace semicolons with commas for proper parsing
        var lines = File.ReadAllLines(semiPath);
        for (int i = 0; i < lines.Length; i++)
        {
            lines[i] = lines[i].Replace(';', ',');
        }
        File.WriteAllLines(tempCsv, lines);
        WorkBook semiWorkbook = WorkBook.LoadCSV(tempCsv, ExcelFileFormat.XLSX);
        // Print both workbooks
        PrintWorkbook(tsvWorkbook, "Tab-delimited File");
        PrintWorkbook(semiWorkbook, "Semicolon-delimited File");
    }
    static void PrintWorkbook(WorkBook workbook, string name)
    {
        Console.WriteLine($"\n--- {name} ---\n");
        DataTable dt = workbook.DefaultWorkSheet.ToDataTable(true);
        // Print column headers
        foreach (DataColumn col in dt.Columns)
        {
            Console.Write($"{col.ColumnName}\t");
        }
        Console.WriteLine();
        // Print rows
        foreach (DataRow row in dt.Rows)
        {
            foreach (var item in row.ItemArray)
            {
                Console.Write($"{item}\t");
            }
            Console.WriteLine();
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code loads CSV files with different delimiters into IronXL WorkBook objects. Tab-delimited files are read using "\t" as the separator, while semicolon-delimited files are converted to standard CSV format before loading. Each workbook’s default worksheet is converted into a DataTable with ToDataTable(true), which uses the first row as column headers.

The PrintWorkbook method then iterates through the DataTable and displays the data in the console, including the headers and all rows. This flexibility addresses many of the common CSV parsing challenges discussed by developers.

Output

As you can see in our console output, both files are correctly formatted into data tables, despite starting out with different delimiters.

How to Read a CSV File into a DataTable in C#: Figure 4 - Different Delimiters output

Conclusion

IronXL transforms the complex task of CSV database imports into a straightforward, reliable process. With just a few lines of code, you can:

  • Read CSV files from any file path
  • Convert CSV data into a new DataTable
  • Preserve column headers and datatable columns
  • Import millions of rows into a database table efficiently

The library handles the intricacies of CSV parsing, memory management, and data type conversions automatically, letting you focus on your business logic rather than file processing details.

Ready to streamline your data import workflow? Start your free trial to find the perfect fit for your team's needs.

常见问题解答

在 C# 中将 CSV 文件读取到 DataTable 的最佳方法是什么?

使用 IronXL,您可以参照我们的开发者指南中的简单代码示例,轻松地将 CSV 文件读取到 C# 中的 DataTable。

为什么我应该使用 IronXL 进行 CSV 到 DataTable 转换?

IronXL 提供简单明了的 API 用于解析 CSV 文件并将其转换为 DataTable,使您可以轻松在 C# 中操作和分析数据。

IronXL 在转换为 DataTable 时能处理大规模的 CSV 文件吗?

是的,IronXL 旨在高效处理大型 CSV 文件并将其转换为 DataTable,而不会出现性能问题。

IronXL 是否支持在将 CSV 转换为 DataTable 后进行数据操作?

当然,一旦您使用 IronXL 将 CSV 文件转换为 DataTable,您可以根据需要轻松操作和分析数据。

我如何使用 IronXL 将 CSV 数据导入到数据库中?

在使用 IronXL 将您的 CSV 文件转换为 DataTable 后,您可以使用 C# 中的标准数据库连接将数据插入到数据库中。

IronXL 适合在企业应用程序中处理 CSV 文件吗?

是的,IronXL 旨在处理企业应用程序中的 CSV 处理任务,提供强大的性能和可靠性。

将 CSV 文件转换为 C# 中的 DataTable 的优势是什么?

将 CSV 文件转换为 DataTable 可以更轻松地进行数据操作、分析和与数据库集成,从而增强您的应用程序的数据处理能力。

除了 CSV 之外,IronXL 能否用于其他电子表格文件类型?

是的,IronXL 支持包括 Excel 格式在内的各种电子表格文件类型,使其在 C# 中具有多功能的数据处理能力。

Curtis Chau
技术作家

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

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