How to Write CSV in C# .NET using IronXL

IronXL provides a simple way to write data to CSV files in .NET by loading Excel workbooks and saving them as CSV format using one line of code, eliminating manual parsing.

Quickstart: Save an Excel Workbook as CSV with IronXL

This example shows how to convert an existing Excel file to CSV using IronXL. Load the workbook and call SaveAsCsv — no manual parsing needed.

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.

    IronXL.WorkBook.Load("MyExcel.xlsx").SaveAsCsv("MyExcel.csv");
  3. Deploy to test on your live environment

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

How to Write CSV in .NET

  • Add the IronXL Library
  • Create a Workbook in C#
  • Save Excel Workbook to CSV file format
How To Work related to How to Write CSV in C# .NET using IronXL

Step 1

How Do I Add IronXL to My Project?

Which Installation Method Should I Use?

If you haven't installed IronXL yet, follow these steps:

  • Open Visual Studio and select the Project menu.
  • Click Manage NuGet Packages.
  • Search for IronXL.Excel.
  • Click Install.

What If I Prefer the Command Line?

Use the following command in the Developer Command Prompt:

Install-Package IronXL.Excel

Where Can I Find More Resources?

For additional tutorials and guidance, visit: https://ironsoftware.com/csharp/excel/docs/. For CSV reading operations, see our guide on how to read CSV files in C#.

Download the sample project here.


How to Tutorial

How Do I Create an Excel Workbook for CSV Export?

What Data Should My Workbook Contain?

CSV (Comma-Separated Values) files are widely used for data exchange between applications due to their simple format and universal compatibility. Whether exporting data for analysis, creating reports, or integrating with other systems, IronXL makes the process seamless.

First, create an Excel workbook containing sample data. If you need help creating Excel files programmatically, visit our tutorial on creating Excel files in .NET:

Excel spreadsheet showing sample data with headers and rows ready for CSV export
Figure 1 - Normal Excel data to be exported to CSV

Which Namespace Do I Need to Import?

Add the IronXL namespace to write CSV files in C#:

using IronXL;
using IronXL;
Imports IronXL
$vbLabelText   $csharpLabel

How Do I Save My Workbook as CSV?

What Code Do I Need to Convert Excel to CSV?

The following C# code demonstrates using the WorkBook object's Load method to load an Excel file, then using the SaveAs method to save it as CSV. This approach handles data types, special characters, and formatting automatically:

:path=/static-assets/excel/content-code-examples/how-to/csharp-write-to-csv-file-save.cs
using IronXL;

// Load the existing Excel workbook
WorkBook wb = WorkBook.Load("Normal_Excel_File.xlsx"); // Import .xls, .csv, or .tsv file

// Save the workbook as a CSV file with the worksheet name appended
wb.SaveAs("Excel_To_CSV.csv"); // Exported as: Excel_To_CSV.Sheet1.csv
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

How Can I Save a Specific Worksheet to CSV?

When working with multi-sheet workbooks, you can export specific worksheets. IronXL provides flexible options for this scenario. Learn more about managing worksheets in our documentation:

using IronXL;

// Load workbook and access specific worksheet
WorkBook workBook = WorkBook.Load("MultiSheet_Excel.xlsx");
WorkSheet sheet = workBook.GetWorkSheet("Sales_Data");

// Save only the specific worksheet as CSV
sheet.SaveAsCsv("Sales_Data_Export.csv");

// Alternatively, save with custom delimiter
sheet.SaveAsCsv("Sales_Data_Tab.tsv", delimiter: "\t");
using IronXL;

// Load workbook and access specific worksheet
WorkBook workBook = WorkBook.Load("MultiSheet_Excel.xlsx");
WorkSheet sheet = workBook.GetWorkSheet("Sales_Data");

// Save only the specific worksheet as CSV
sheet.SaveAsCsv("Sales_Data_Export.csv");

// Alternatively, save with custom delimiter
sheet.SaveAsCsv("Sales_Data_Tab.tsv", delimiter: "\t");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

What Does the CSV Output Look Like?

The output CSV file appears as follows when opened in a text editor. Notice how IronXL properly formats the data with commas as delimiters and handles text fields containing special characters:

Text editor view of CSV file showing comma-separated data with proper formatting and structure
Figure 2 - Output CSV file

How Do I Handle Multiple Worksheets?

When your Excel file contains multiple worksheets, IronXL offers several approaches for CSV export. Explore more about converting spreadsheet file types in our comprehensive guide:

using IronXL;
using System;

// Load workbook with multiple sheets
WorkBook workBook = WorkBook.Load("MultipleSheets.xlsx");

// Method 1: Export all sheets to separate CSV files
foreach (WorkSheet sheet in workBook.WorkSheets)
{
    string fileName = $"{sheet.Name}_Export.csv";
    sheet.SaveAsCsv(fileName);
    Console.WriteLine($"Exported {sheet.Name} to {fileName}");
}

// Method 2: Combine all sheets into one CSV
var combinedData = workBook.GetWorkSheet(0);
for (int i = 1; i < workBook.WorkSheets.Count; i++)
{
    var currentSheet = workBook.GetWorkSheet(i);
    // Add logic to append data from currentSheet to combinedData
}
combinedData.SaveAsCsv("Combined_Output.csv");
using IronXL;
using System;

// Load workbook with multiple sheets
WorkBook workBook = WorkBook.Load("MultipleSheets.xlsx");

// Method 1: Export all sheets to separate CSV files
foreach (WorkSheet sheet in workBook.WorkSheets)
{
    string fileName = $"{sheet.Name}_Export.csv";
    sheet.SaveAsCsv(fileName);
    Console.WriteLine($"Exported {sheet.Name} to {fileName}");
}

// Method 2: Combine all sheets into one CSV
var combinedData = workBook.GetWorkSheet(0);
for (int i = 1; i < workBook.WorkSheets.Count; i++)
{
    var currentSheet = workBook.GetWorkSheet(i);
    // Add logic to append data from currentSheet to combinedData
}
combinedData.SaveAsCsv("Combined_Output.csv");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

What About Performance Considerations?

For large Excel files, IronXL provides efficient streaming capabilities that minimize memory usage. When working with substantial datasets, use IronXL's optimized methods. See our guide on exporting data to various formats for advanced scenarios.

Can I Work with DataTables?

IronXL seamlessly integrates with .NET's DataTable structure, making it easy to export data from databases or in-memory collections. See our tutorial on converting DataTable to CSV:

using IronXL;
using System.Data;

// Create a DataTable with sample data
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Price", typeof(decimal));

// Add sample rows
dataTable.Rows.Add(1, "Product A", 29.99m);
dataTable.Rows.Add(2, "Product B", 49.99m);

// Convert DataTable to WorkSheet
WorkBook workBook = WorkBook.Create();
WorkSheet sheet = workBook.CreateWorkSheet("Products");
sheet.InsertDataTable(dataTable, "A1");

// Save as CSV
workBook.SaveAsCsv("Products.csv");
using IronXL;
using System.Data;

// Create a DataTable with sample data
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Price", typeof(decimal));

// Add sample rows
dataTable.Rows.Add(1, "Product A", 29.99m);
dataTable.Rows.Add(2, "Product B", 49.99m);

// Convert DataTable to WorkSheet
WorkBook workBook = WorkBook.Create();
WorkSheet sheet = workBook.CreateWorkSheet("Products");
sheet.InsertDataTable(dataTable, "A1");

// Save as CSV
workBook.SaveAsCsv("Products.csv");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

How Do I Set Up Licensing?

Before deploying your CSV export solution, configure your IronXL license properly. Visit our guide on using license keys to learn about applying licenses in different environments.


Library Quick Access

IronXL API Reference Documentation

Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the IronXL API Reference Documentation. For detailed method signatures and additional CSV export options, consult our [API Reference](https://ironsoftware.com/csharp/excel/object-reference/api/).

IronXL API Reference Documentation
Documentation related to Library Quick Access

Frequently Asked Questions

How do I write CSV files in C# without manual parsing?

IronXL provides a simple one-line solution to write CSV files in C# by loading Excel workbooks and saving them as CSV format using the SaveAsCsv method, eliminating the need for manual parsing or complex string manipulation.

What is the quickest way to convert Excel to CSV in .NET?

With IronXL, you can convert an Excel file to CSV in just one line of code: IronXL.WorkBook.Load("MyExcel.xlsx").SaveAsCsv("MyExcel.csv"). This handles all data types, special characters, and formatting automatically.

How do I install the library for CSV file writing in C#?

You can install IronXL through Visual Studio's NuGet Package Manager by searching for 'IronXL.Excel' or use the Developer Command Prompt with the appropriate installation command.

Which namespace do I need to import for CSV operations?

To write CSV files using IronXL, you need to import the IronXL namespace by adding 'using IronXL;' at the top of your C# file.

Can I save a specific worksheet to CSV format?

Yes, IronXL allows you to save individual worksheets from an Excel workbook to CSV format, giving you flexibility to export only the data you need rather than the entire workbook.

What data formats are supported when converting to CSV?

IronXL automatically handles various data types including text, numbers, dates, and special characters when converting Excel workbooks to CSV format, ensuring data integrity throughout the conversion process.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 1,753,501 | Version: 2025.12 just released