Published February 12, 2023
How to Create CSV File in C# (Step-By-Step) Tutorial
Looking for how to create/write new CSV file data format using C# .NET class library in Console Application? If yes, then you are on the right place.
This is the simple tutorial written for creating multi-row CSV files (with headings) from strings using the IronXL C#.NET library. IronXL is the market leading .NET library for handling documents like Excel, CSV and XLS without any additional user configuration.
About CSV Files
CSV (or Comma Separated Values) is a well-known data format used in databases and spreadsheets by users. Commas are used to separate rows of strings when information is retained on a single line. Using this format, thousands of lines of text separated by newline can be read in mere seconds. Furthermore, it is portable and light enough to be easily moved between devices.
How can I store/parse data in a CSV file?
In this context, CSV files can be separated by commas. All the written rows should be saved separately and terminated by newline when the files are loaded. The columns should be formatted using two commas to separate the values or points for users.
How can I create and write CSV files using C#?
Here is simplest method on generating and editing CSV files using C#, and you can expand upon it for specific writing needs. First, you must create a new C# Console app and follow the following steps.
All you need to get started is Visual Studio, and we'll walk you through installing the IronXL C#.NET library using the Package Manager below.
1. Starting a New Project in Visual Studio
Open the Visual Studio IDE.
Go to the File menu, and select Console Application after choosing "New Project."
Enter the Project Name and the preferred project location in the appropriate text box.

Create a New C#.NET Console Project
Click the Next button, and select the required .NET Framework, as shown in the screenshot below, and click on Create.

Select a .NET Framework for the New C#.NET Console Project
The program.cs file will open.

The code for this sample project will be added to the program.cs file
2. Install the IronXL C# Library
The IronXL library can be downloaded and installed in many different ways but we will only discuss two of those:
- Using the Visual Studio NuGet Package Manager
- Using the Visual Studio Command-Line
2.1. Using the Visual Studio NuGet Package Manager
The NuGet Package Manager option is available in the Visual Studio software to install the package directly into the solution. The below screenshot shows how to open it.

Accessing Visual Studio's NuGet Package Manager UI Feature
The NuGet Package Manager feature provides an area where users can browse and search for packages that are offered on the NuGet website. Enter "IronXL" in the search field to find the IronXL library.

Locate the IronXL library in Visual Studio's NuGet Package Manager UI by searching for it in the Browse section
We can see the list of linked packages from the search in the image up above. We need to select the IronXL option and install the package to our solution.
2.2. Using the Visual Studio Command-Line
In Visual Studio menu go to Tools, move the cursor to NuGet Package Manager and click on Package Manager Console.

You can install the IronXL C# Excel Library via the commandline using the Install-Package IronXL.Excel
NuGet Console command.
Package Manager Console will appear in the bottom of the screen. Just write the following code command and press enter, IronXL will install an instance.
Install-Package IronXL.Excel
3. Creating CSV Files
In this blog, we will discuss about two different methods for creating a CSV file using IronXL.
- Create CSV from XLSX file
- Create CSV file by creating new workbook and write data
3.1. Create CSV from XLSX file
For creating CSV file, we need an Excel file containing some example data. For this example, we will use the following XLSX file to create a CSV file.

A sample .XLSX Excel File that will be transformed into an CSV file
Enter the code given below in the opened program.cs file and run the project.
using IronXL;
WorkBook wb = WorkBook.Load("test.xlsx");
wb.SaveAsCsv("Parsed CSV.csv");
using IronXL;
WorkBook wb = WorkBook.Load("test.xlsx");
wb.SaveAsCsv("Parsed CSV.csv");
Imports IronXL
Private wb As WorkBook = WorkBook.Load("test.xlsx")
wb.SaveAsCsv("Parsed CSV.csv")
After execution is completed, a new file named Parsed CSV.csv file will be created. Open the file using your preferred Spreadsheet application.

The resulting CSV File generated from an .XLSX file using IronXL
3.2. Create new CSV Workbook Programmatically
Here, we will discuss about how to create CSV file from scratch using IronXL.
Let's get started with an example code.
using IronXL;
WorkBook xlsWorkbook = WorkBook.Create(ExcelFileFormat.XLSX);
xlsWorkbook.Metadata.Author = "IronXL";
WorkSheet xlsSheet = xlsWorkbook.CreateWorkSheet("new_sheet");
xlsSheet["A1"].Value = "Hello";
xlsSheet["B1"].Value = "World";
xlsSheet["A2"].Value = "New CSV example output";
xlsSheet["B2"].Value = "From Scratch";
xlsWorkbook.SaveAsCsv("NewCSVFile.csv");
using IronXL;
WorkBook xlsWorkbook = WorkBook.Create(ExcelFileFormat.XLSX);
xlsWorkbook.Metadata.Author = "IronXL";
WorkSheet xlsSheet = xlsWorkbook.CreateWorkSheet("new_sheet");
xlsSheet["A1"].Value = "Hello";
xlsSheet["B1"].Value = "World";
xlsSheet["A2"].Value = "New CSV example output";
xlsSheet["B2"].Value = "From Scratch";
xlsWorkbook.SaveAsCsv("NewCSVFile.csv");
Imports IronXL
Private xlsWorkbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
xlsWorkbook.Metadata.Author = "IronXL"
Dim xlsSheet As WorkSheet = xlsWorkbook.CreateWorkSheet("new_sheet")
xlsSheet("A1").Value = "Hello"
xlsSheet("B1").Value = "World"
xlsSheet("A2").Value = "New CSV example output"
xlsSheet("B2").Value = "From Scratch"
xlsWorkbook.SaveAsCsv("NewCSVFile.csv")
The sample code above first creates a new workbook, and then populates the rows and columns of the workbook with data. Afterwards, it saves the file using the SaveAsCsv
method.

The creation of a new Excel workbook with data using IronXL
4. Conclusion
In this article, we learned about how to create a new CSV file using C#.NET library IronXL using two different methods.
For more information about IronXL, browse the Code Examples pages. Download IronXL and try it out for 30-days for free before buying a license.
Buy the complete Iron Suite to get IronXL and four other products for the same price as two IronXL licenses.