# 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.
*as-heading:2(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.
```cs
:title=Convert to CSV in One Simple Step
IronXL.WorkBook.Load("MyExcel.xlsx").SaveAsCsv("MyExcel.csv");
```
<div class="learnn-how-section">
<div class="row">
<div class="col-sm-6">
<h2>How to Write CSV in .NET</h2>
<ul class="list-unstyled">
<li>Add the IronXL Library</li>
<li>Create a Workbook in C#</li>
<li>Save Excel Workbook to CSV file format</li>
</ul>
</div>
<div class="col-sm-6">
<div class="download-card">
<img style="box-shadow: none; width: 308px; height: 320px;" src="/img/faq/excel/how-to-work.svg" class="img-responsive learn-how-to-img replaceable-img" />
</div>
</div>
</div>
</div>
<hr class="separator" />
<h4 class="tutorial-segment-title">Step 1</h4>
## 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:
```shell
:ProductInstall
```
### Where Can I Find More Resources?
For additional tutorials and guidance, visit: [https://ironsoftware.com/csharp/excel/docs/](https://ironsoftware.com/csharp/excel/docs/). For CSV reading operations, see our guide on [how to read CSV files in C#](https://ironsoftware.com/csharp/excel/how-to/csharp-read-csv-file/). Download the sample project [here](https://ironsoftware.com/csharp/excel/downloads/csharp-write-to-csv.zip).
<hr class="separator" />
<h4 class="tutorial-segment-title">How to Tutorial</h4>
## 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 smooth.
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](https://ironsoftware.com/csharp/excel/tutorials/create-excel-file-net/):
<center>
<div class="center-image-wrapper">
<a
href="/img/faq/excel/csharp-write-to-csv-file/normal-excel-data-to-be-exported-to-csv.png"
target="_blank"
>
<img
class="img-responsive"
src="/img/faq/excel/csharp-write-to-csv-file/normal-excel-data-to-be-exported-to-csv.png"
alt="Excel spreadsheet showing sample data with headers and rows ready for CSV export" />
</a>
<div class="image-description">
<span class="image-description-text_strong">
Figure 1
</span>
<span class="image-description-text_regular">
-
</span>
<span class="image-description-text_italic">
Normal Excel data to be exported to CSV
</span>
</div>
</div>
</center>
### Which Namespace Do I Need to Import?
Add the `IronXL` namespace to write CSV files in C#:
```csharp
using IronXL;
```
<hr class="separator" />
## 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:
```csharp
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
```
### 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](https://ironsoftware.com/csharp/excel/how-to/manage-worksheet/) in our documentation:
```csharp
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");
```
### 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:
<center>
<div class="center-image-wrapper">
<a
href="/img/faq/excel/csharp-write-to-csv-file/output-csv-file.png"
target="_blank"
>
<img
class="img-responsive"
src="/img/faq/excel/csharp-write-to-csv-file/output-csv-file.png"
alt="Text editor view of CSV file showing comma-separated data with proper formatting and structure" />
</a>
<div class="image-description">
<span class="image-description-text_strong">
Figure 2
</span>
<span class="image-description-text_regular">
-
</span>
<span class="image-description-text_italic">
Output CSV file
</span>
</div>
</div>
</center>
### 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](https://ironsoftware.com/csharp/excel/how-to/convert-spreadsheet-file-types/) in our guide:
```csharp
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.WorkSheets[0];
for (int i = 1; i < workBook.WorkSheets.Count; i++)
{
var currentSheet = workBook.WorkSheets[i];
// Add logic to append data from currentSheet to combinedData
}
combinedData.SaveAsCsv("Combined_Output.csv");
```
### 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](https://ironsoftware.com/csharp/excel/how-to/c-sharp-export-to-excel/) for advanced scenarios.
### Can I Work with DataTables?
IronXL 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](https://ironsoftware.com/csharp/excel/how-to/csharp-datatable-to-csv/):
```csharp
using IronXL;
using System.Data;
// Create a DataTable with sample data (the table name becomes the worksheet name)
DataTable dataTable = new DataTable("Products");
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);
// Wrap the DataTable in a DataSet — each table is loaded as its own worksheet
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dataTable);
// Load the DataSet into a new workbook
WorkBook workBook = WorkBook.Create();
WorkBook.LoadWorkSheetsFromDataSet(dataSet, workBook);
// Save as CSV
workBook.SaveAsCsv("Products.csv");
```
### How Do I Set Up Licensing?
Before deploying your CSV export solution, configure your IronXL license properly. Visit our guide on [using license keys](https://ironsoftware.com/csharp/excel/get-started/license-keys/) to learn about applying licenses in different environments.
<hr class="separator" />
<h4 class="tutorial-segment-title">Library Quick Access</h4>
<div class="tutorial-section">
<div class="row">
<div class="col-sm-8">
<h3>IronXL API Reference Documentation</h3>
<p>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 <a href="https://ironsoftware.com/csharp/excel/object-reference/api/" target="_blank">API Reference</a>.</p>
<a class="doc-link" href="/csharp/excel/object-reference/api/" target="_blank"> IronXL API Reference Documentation <i class="fa fa-chevron-right"></i></a>
Also refer to our [API Reference](https://ironsoftware.com/csharp/excel/object-reference/api/) for full details.
</div>
<div class="col-sm-4">
<div class="tutorial-image">
<img style="max-width: 110px; width: 100px; height: 140px;" alt="" class="img-responsive add-shadow" src="/img/svgs/documentation.svg" width="100" height="140" />
</div>
</div>
</div>
</div>
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.
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 smooth.
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:
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;
ImportsIronXL
Imports IronXL
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:
using IronXL;// Load the existing Excel workbookWorkBook wb = WorkBook.Load("Normal_Excel_File.xlsx"); // Import .xls, .csv, or .tsv file// Save the workbook as a CSV file with the worksheet name appendedwb.SaveAs("Excel_To_CSV.csv"); // Exported as: Excel_To_CSV.Sheet1.csv
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
ImportsIronXL' Load the existing Excel workbookDim wb AsWorkBook = WorkBook.Load("Normal_Excel_File.xlsx") ' Import .xls, .csv, or .tsv file' Save the workbook as a CSV file with the worksheet name appendedwb.SaveAs("Excel_To_CSV.csv") ' Exported as: Excel_To_CSV.Sheet1.csv
Imports IronXL
' Load the existing Excel workbook
Dim wb As WorkBook = 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
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 worksheetWorkBook workBook = WorkBook.Load("MultiSheet_Excel.xlsx");WorkSheet sheet = workBook.GetWorkSheet("Sales_Data");// Save only the specific worksheet as CSVsheet.SaveAsCsv("Sales_Data_Export.csv");// Alternatively, save with custom delimitersheet.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");
ImportsIronXL' Load workbook and access specific worksheetDim workBook AsWorkBook = WorkBook.Load("MultiSheet_Excel.xlsx")Dim sheet AsWorkSheet = workBook.GetWorkSheet("Sales_Data")' Save only the specific worksheet as CSVsheet.SaveAsCsv("Sales_Data_Export.csv")' Alternatively, save with custom delimitersheet.SaveAsCsv("Sales_Data_Tab.tsv", delimiter:=vbTab)
Imports IronXL
' Load workbook and access specific worksheet
Dim workBook As WorkBook = WorkBook.Load("MultiSheet_Excel.xlsx")
Dim sheet As WorkSheet = 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:=vbTab)
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:
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 guide:
using IronXL;using System;// Load workbook with multiple sheetsWorkBook workBook = WorkBook.Load("MultipleSheets.xlsx");// Method 1: Export all sheets to separate CSV filesforeach (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 CSVvar combinedData = workBook.WorkSheets[0];for (int i = 1; i < workBook.WorkSheets.Count; i++){ var currentSheet = workBook.WorkSheets[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.WorkSheets[0];
for (int i = 1; i < workBook.WorkSheets.Count; i++)
{
var currentSheet = workBook.WorkSheets[i];
// Add logic to append data from currentSheet to combinedData
}
combinedData.SaveAsCsv("Combined_Output.csv");
C#
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 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 (the table name becomes the worksheet name)DataTable dataTable = new DataTable("Products");dataTable.Columns.Add("ID", typeof(int));dataTable.Columns.Add("Name", typeof(string));dataTable.Columns.Add("Price", typeof(decimal));// Add sample rowsdataTable.Rows.Add(1, "Product A", 29.99m);dataTable.Rows.Add(2, "Product B", 49.99m);// Wrap the DataTable in a DataSet — each table is loaded as its own worksheetDataSet dataSet = new DataSet();dataSet.Tables.Add(dataTable);// Load the DataSet into a new workbookWorkBook workBook = WorkBook.Create();WorkBook.LoadWorkSheetsFromDataSet(dataSet, workBook);// Save as CSVworkBook.SaveAsCsv("Products.csv");
using IronXL;
using System.Data;
// Create a DataTable with sample data (the table name becomes the worksheet name)
DataTable dataTable = new DataTable("Products");
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);
// Wrap the DataTable in a DataSet — each table is loaded as its own worksheet
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dataTable);
// Load the DataSet into a new workbook
WorkBook workBook = WorkBook.Create();
WorkBook.LoadWorkSheetsFromDataSet(dataSet, workBook);
// Save as CSV
workBook.SaveAsCsv("Products.csv");
C#
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.
What is IronXL and how can it help me write CSV files in C#?
IronXL is a library that helps .NET developers write CSV files by converting Excel workbooks to CSV format with minimal code, avoiding manual parsing.
How do I convert an Excel file to CSV using IronXL?
You can convert an Excel file to CSV using IronXL by loading the Excel workbook and calling the `SaveAsCsv` method. For example: `IronXL.WorkBook.Load("MyExcel.xlsx").SaveAsCsv("MyExcel.csv");`.
What steps are needed to add IronXL to my C# project?
To add IronXL to your project, open Visual Studio, go to Project > Manage NuGet Packages, search for `IronXL.Excel`, and click Install.
Can IronXL handle Excel files with multiple worksheets?
Yes, IronXL can handle multiple worksheets. You can export each worksheet individually to a CSV file or combine them into a single CSV file.
What are the benefits of using IronXL for large Excel files?
IronXL provides efficient streaming capabilities that minimize memory usage, making it suitable for exporting large datasets to CSV without performance issues.
How do I save a specific worksheet from an Excel file to a CSV using IronXL?
To save a specific worksheet to a CSV, load the workbook, access the worksheet you want, and use `SaveAsCsv` on that worksheet, e.g., `sheet.SaveAsCsv("SpecificWorksheet.csv");`.
Can IronXL integrate with .NET's DataTable for CSV export?
Yes, IronXL integrates with .NET's DataTable, which allows you to export data from databases or in-memory collections easily to CSV format.
Is it necessary to configure a license for using IronXL?
Yes, before deploying your solution, you should properly configure your IronXL license by visiting the guide on using license keys provided by Iron Software.
What namespace is required to use IronXL for CSV writing?
You need to import the `IronXL` namespace in your C# project to write CSV files with IronXL.
Where can I find more resources and documentation for using IronXL?
Additional resources and detailed API documentation are available at Iron Software’s website, specifically at [IronXL API Reference](https://ironsoftware.com/csharp/excel/object-reference/api/).
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.