How to Write CSV in .NET

Ever wondered how to quickly use C# to write to CSV? Wonder no more! IronXL provides a very quick and easy way to write data into CSV files in .NET.


Step 1

1. Add IronXL to Your Project

Just in case you haven’t installed IronXL yet, here are the quick steps you need to take:

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

Or use the following command in the Developer Command Prompt:

Install-Package IronXL.Excel

If you would like further guidance from any of our tutorials, please follow this link: https://ironsoftware.com/csharp/excel/docs/

You can even download the file project here.


How to Tutorial

2. Create an Excel Workbook

Let’s create a quick project!

First, create an Excel workbook containing the following information:

Normal Excel data to be exported to CSV
Figure 1 - Normal Excel data to be exported to CSV

Then add the IronXL Namespace in order to be able to write to CSV files in C# using IronXL:

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

3. Save Workbook to CSV

The following C# code demonstrates how to use the WorkBook object's Load method to load an Excel file. It then uses the SaveAs method to save the file as a CSV:

// Method to save an Excel workbook as a CSV file
private void button3_Click(object sender, EventArgs e)
{
    // 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
}
// Method to save an Excel workbook as a CSV file
private void button3_Click(object sender, EventArgs e)
{
    // 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
}
' Method to save an Excel workbook as a CSV file
Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs)
	' 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
End Sub
$vbLabelText   $csharpLabel

The output CSV file appears as follows when opened in a text editor such as Notepad.

Output CSV file
Figure 2 - Output CSV file

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 Library Quick Access

Frequently Asked Questions

What is IronXL?

IronXL is a library that allows developers to work with Excel files in .NET applications, including reading, writing, and converting data such as CSV files.

How do I add IronXL to my C# project?

To add IronXL to your C# project, open Visual Studio, select the Project menu, click on Manage NuGet Packages, search for IronXL.Excel, and click Install. Alternatively, you can use the command line to run Install-Package IronXL.Excel.

How can I create an Excel workbook using C#?

You can create an Excel workbook in C# by using the IronXL library to define and manipulate the structure and data of the workbook programmatically.

How do I save an Excel workbook as a CSV file in C#?

To save an Excel workbook as a CSV file in C#, use the WorkBook object's Load method to load the Excel file, then use the SaveAs method to export it as a CSV file.

Can I use IronXL to load and save CSV files?

Yes, IronXL supports loading and saving CSV files, allowing you to easily convert between Excel and CSV formats.

Is there a way to access IronXL documentation for more information?

Yes, you can access the IronXL API Reference Documentation online, which provides detailed information and examples for using IronXL functionalities.

Are there any downloadable resources available for learning IronXL?

Yes, you can download a file project from the IronXL website that demonstrates how to write to a CSV file using C# and IronXL.

What kind of data can be exported using IronXL?

IronXL allows for the export of various types of Excel data into CSV format, as demonstrated in the tutorial with a sample Excel workbook.

How does IronXL simplify working with Excel files in .NET?

IronXL simplifies working with Excel files in .NET by providing easy-to-use methods for reading, writing, and converting Excel data, reducing the complexity typically involved in handling Excel file operations.

What command is used to install IronXL via the Developer Command Prompt?

To install IronXL via the Developer Command Prompt, use the command: Install-Package IronXL.Excel.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.