Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Looking for how to create/write a new CSV file data format using a C# .NET library in a Console Application? If yes, then you are in the right place.
This is a 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.
The simplest method for generating and editing CSV files using C#, is following those simple steps:
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.
Configure New Project
Click the Next button, select the required .NET Framework, as shown in the screenshot below, and click on Create.
Target Framework Selection
The program.cs file will open.
Program.cs
The IronXL library can be downloaded and installed in two different ways:
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.
NuGet Package Manager
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.
NuGet Package Manager
From the linked packages from the search in the image up above, select the IronXL option and install the package to the solution.
In the Visual Studio menu go to Tools, move the cursor to NuGet Package Manager and click on Package Manager Console.
Visual Studio Commandline
Package Manager Console will appear at the bottom of the screen. Just write the following code command and press enter, IronXL will install an instance.
Install-Package IronXL.Excel
In this blog, there are two different methods for creating a CSV file using IronXL.
WorkBook
and writing dataFor creating a CSV file, the Excel file should contain some example data.
Sample Excel 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.
CSV Data
This example code shows how to create a CSV file from scratch using IronXL.
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. Afterward, it saves the file using the SaveAsCsv
method.
CSV File Output
This article demonstrated how to create a new CSV file using the C# .NET library IronXL using two different methods.
Additionally, the IronXL Excel library provides the following features:
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.
9 .NET API products for your office documents