Read a CSV File in C#
IronXL provides a one-line solution to read CSV files in C# using the LoadCSV method. It supports custom delimiters and direct conversion to Excel formats for seamless data processing in .NET applications.
Quickstart: Load and convert a CSV file using IronXL in one line
This example shows how to read a CSV file using IronXL's LoadCSV method and save it as an Excel workbook with minimal code.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
WorkBook wb = WorkBook.LoadCSV("data.csv", ExcelFileFormat.XLSX, listDelimiter: ","); wb.SaveAs("output.xlsx");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download and install the C# CSV reading library
- Create a C# or VB project
- Add the code example from this page to your project
- Specify the CSV path and output name & format
- Run the project to view the document
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
Step 1
How Do I Install the IronXL Library?
Before using IronXL to read CSV files in MVC, ASP, or .NET Core, you need to install it. Here's a quick walkthrough.
Why Should I Use NuGet Package Manager?
- In Visual Studio, select the Project menu
- Manage NuGet Packages
- Search for IronXL.Excel
- Install
What Are Alternative Installation Methods?
Or download from the Iron Software website: https://ironsoftware.com/csharp/excel/packages/IronXL.zip
For .NET developers working with Docker containers, IronXL can be configured in your Docker environment. The library also supports deployment on Azure Functions and AWS Lambda for cloud-based CSV processing.
How to Tutorial
How Do I Read CSV Files Programmatically?
Now for the project!
What Namespace Do I Need to Import?
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 IronXLHow Do I Load and Convert CSV Files?
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.comWhat Advanced CSV Reading Options Are Available?
IronXL provides features for handling CSV files with various configurations. You can specify different delimiters (semicolons, tabs, pipes) and handle files with different encodings:
// Example: Reading CSV with custom delimiter and encoding
WorkBook workbook = WorkBook.LoadCSV("data.csv",
fileFormat: ExcelFileFormat.XLSX,
listDelimiter: ";", // Using semicolon as delimiter
encoding: Encoding.UTF8);
// Access specific cells after loading
var cellValue = workbook.DefaultWorkSheet["A1"].Value;
// Iterate through rows
foreach (var row in workbook.DefaultWorkSheet.Rows)
{
// Process each row
foreach (var cell in row)
{
Console.WriteLine(cell.Value);
}
}// Example: Reading CSV with custom delimiter and encoding
WorkBook workbook = WorkBook.LoadCSV("data.csv",
fileFormat: ExcelFileFormat.XLSX,
listDelimiter: ";", // Using semicolon as delimiter
encoding: Encoding.UTF8);
// Access specific cells after loading
var cellValue = workbook.DefaultWorkSheet["A1"].Value;
// Iterate through rows
foreach (var row in workbook.DefaultWorkSheet.Rows)
{
// Process each row
foreach (var cell in row)
{
Console.WriteLine(cell.Value);
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comWhat Does the CSV File Look Like Before Processing?
How Does LoadCSV Method Work?
A Workbook object is created. The LoadCSV method of the Workbook object specifies the CSV file to read, the format to read it into, and the delimiter. In this case, a comma is used as a separator.
A Worksheet object is created where the CSV contents are placed. The file is then saved under a new name and format. This process is useful when you need to convert between different spreadsheet formats.
Can I Process Large CSV Files Efficiently?
IronXL is optimized for performance and handles large CSV files efficiently. For developers working with substantial datasets, the library offers significant performance improvements in recent versions. When processing large files, consider these best practices:
// Reading large CSV files with memory optimization
WorkBook workbook = WorkBook.LoadCSV("large_dataset.csv",
fileFormat: ExcelFileFormat.XLSX,
listDelimiter: ",");
// Process data in chunks
var worksheet = workbook.DefaultWorkSheet;
int rowCount = worksheet.RowCount;
int batchSize = 1000;
for (int i = 0; i < rowCount; i += batchSize)
{
// Process rows in batches
var endIndex = Math.Min(i + batchSize, rowCount);
for (int j = i; j < endIndex; j++)
{
var row = worksheet.GetRow(j);
// Process individual row
}
}// Reading large CSV files with memory optimization
WorkBook workbook = WorkBook.LoadCSV("large_dataset.csv",
fileFormat: ExcelFileFormat.XLSX,
listDelimiter: ",");
// Process data in chunks
var worksheet = workbook.DefaultWorkSheet;
int rowCount = worksheet.RowCount;
int batchSize = 1000;
for (int i = 0; i < rowCount; i += batchSize)
{
// Process rows in batches
var endIndex = Math.Min(i + batchSize, rowCount);
for (int j = i; j < endIndex; j++)
{
var row = worksheet.GetRow(j);
// Process individual row
}
}IRON VB CONVERTER ERROR developers@ironsoftware.comHow Can I Export CSV Data to Other Formats?
After reading CSV files, you might need to export the data to various formats. IronXL supports multiple export options including XLSX to CSV conversion, JSON, XML, and HTML. Here's how to export to different formats:
// Load CSV and export to multiple formats
WorkBook workbook = WorkBook.LoadCSV("input.csv", ExcelFileFormat.XLSX, ",");
// Export to different formats
workbook.SaveAs("output.xlsx"); // Excel format
workbook.SaveAsJson("output.json"); // JSON format
workbook.SaveAsXml("output.xml"); // XML format
// Export specific worksheet to CSV with custom delimiter
workbook.DefaultWorkSheet.SaveAs("output_custom.csv", ";");// Load CSV and export to multiple formats
WorkBook workbook = WorkBook.LoadCSV("input.csv", ExcelFileFormat.XLSX, ",");
// Export to different formats
workbook.SaveAs("output.xlsx"); // Excel format
workbook.SaveAsJson("output.json"); // JSON format
workbook.SaveAsXml("output.xml"); // XML format
// Export specific worksheet to CSV with custom delimiter
workbook.DefaultWorkSheet.SaveAs("output_custom.csv", ";");IRON VB CONVERTER ERROR developers@ironsoftware.comWhat About Working with CSV Data in Web Applications?
For ASP.NET developers, IronXL provides seamless integration for reading CSV files in web applications. You can upload and process CSV files in your MVC or Web API projects:
// Example: Processing uploaded CSV file in ASP.NET
public ActionResult UploadCSV(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
// Save uploaded file temporarily
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/"), fileName);
file.SaveAs(path);
// Load and process CSV
WorkBook workbook = WorkBook.LoadCSV(path, ExcelFileFormat.XLSX, ",");
// Convert to DataTable for easy display
var dataTable = workbook.DefaultWorkSheet.ToDataTable();
// Clean up temporary file
System.IO.File.Delete(path);
return View(dataTable);
}
return RedirectToAction("Index");
}// Example: Processing uploaded CSV file in ASP.NET
public ActionResult UploadCSV(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
// Save uploaded file temporarily
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/"), fileName);
file.SaveAs(path);
// Load and process CSV
WorkBook workbook = WorkBook.LoadCSV(path, ExcelFileFormat.XLSX, ",");
// Convert to DataTable for easy display
var dataTable = workbook.DefaultWorkSheet.ToDataTable();
// Clean up temporary file
System.IO.File.Delete(path);
return View(dataTable);
}
return RedirectToAction("Index");
}IRON VB CONVERTER ERROR developers@ironsoftware.comHow Do I Handle CSV Files with Complex Data?
When working with CSV files containing formulas, special characters, or mixed data types, IronXL provides robust handling capabilities. You can work with formulas and format cell data appropriately:
// Handle CSV with special requirements
WorkBook workbook = WorkBook.LoadCSV("complex_data.csv",
ExcelFileFormat.XLSX,
listDelimiter: ",");
var worksheet = workbook.DefaultWorkSheet;
// Apply formatting to cells
worksheet["A1:A10"].Style.Font.Bold = true;
worksheet["B1:B10"].FormatString = "$#,##0.00"; // Currency format
// Add formulas after loading CSV data
worksheet["D1"].Formula = "=SUM(B1:B10)";// Handle CSV with special requirements
WorkBook workbook = WorkBook.LoadCSV("complex_data.csv",
ExcelFileFormat.XLSX,
listDelimiter: ",");
var worksheet = workbook.DefaultWorkSheet;
// Apply formatting to cells
worksheet["A1:A10"].Style.Font.Bold = true;
worksheet["B1:B10"].FormatString = "$#,##0.00"; // Currency format
// Add formulas after loading CSV data
worksheet["D1"].Formula = "=SUM(B1:B10)";IRON VB CONVERTER ERROR developers@ironsoftware.comLibrary 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 DocumentationFrequently Asked Questions
How do I read CSV files in C# quickly?
IronXL provides a one-line solution to read CSV files in C# using the LoadCSV method. Simply use: WorkBook wb = WorkBook.LoadCSV("data.csv", ExcelFileFormat.XLSX, listDelimiter: ","); This loads your CSV file and allows you to save it as an Excel workbook with wb.SaveAs("output.xlsx").
What installation methods are available for the CSV reading library?
You can install IronXL through NuGet Package Manager in Visual Studio by searching for 'IronXL.Excel', or download it directly from the Iron Software website. The library also supports Docker containers, Azure Functions, and AWS Lambda for cloud-based CSV processing.
Can I use custom delimiters when reading CSV files?
Yes, IronXL supports various delimiters including semicolons, tabs, and pipes. You can specify the delimiter using the listDelimiter parameter in the LoadCSV method, for example: WorkBook.LoadCSV("data.csv", ExcelFileFormat.XLSX, listDelimiter: ";").
What namespace do I need to import for CSV reading functionality?
You need to add 'using IronXL;' at the top of your C# file to access all IronXL functionalities for reading and processing CSV files.
How can I access specific cell values after loading a CSV file?
After loading a CSV file with IronXL's LoadCSV method, you can access specific cells using: var cellValue = workbook.DefaultWorkSheet["A1"].Value; This allows you to retrieve and manipulate individual cell data from your loaded CSV.
Does the library support different file encodings when reading CSV files?
Yes, IronXL supports various encodings including UTF8. You can specify the encoding parameter when loading CSV files: WorkBook.LoadCSV("data.csv", fileFormat: ExcelFileFormat.XLSX, encoding: Encoding.UTF8).
Can I convert CSV files to Excel format directly?
Yes, IronXL allows direct conversion from CSV to Excel formats. After loading a CSV file with LoadCSV, you can immediately save it as an Excel file using the SaveAs method, specifying formats like XLSX or XLS.









