Writing CSV files using CSVHelper package (C#, IEnumerable)

This is a follow-up on my post on how to read a CSV file using Josh Close’s CsvHelper.  CsvHelper is a fast and flexible .NET library for reading and writing CSV files.

Below I will now show how to write CSV files using CsvHelper.  The example will actually be reading a CSV file and then writing the contents of that CSV into another but doing it three times.  Each time we do this we will use another method (WriteRecords for all records at once, WriteRecord for writing one record, and WriteField for writing one field).

Our sample input CSV file will look like the following.

CommonName,FormalName,TelephoneCode,CountryCode

Argentina,Argentine Republic,54,ARG
Armenia,Republic of Armenia,374,ARM
Australia,Commonwealth of Australia,61,AUS
Austria,Republic of Austria,43,AUT
“Bahamas, The”,Commonwealth of The Bahamas,-241,BHS
Bangladesh,People’s Republic of Bangladesh,880,BGD

Note this CSV file is a simpler file based on the file which is located here.

The first thing to do is to Install CsvHelper.  To do this run the following command in the Package Manager Console:

Now add CsvHelper to the program by adding:

The next step is to create a class which has properties with the same name of the column headings found in the csv file.  Below you will find an example of a class which does this:

Finally create an instance of CSVReader and CSVWriter  and invoke the GetRecords method using the DataRecord class to read the CSV file. 

Once we have the completed we are now ready to write the file out.  We first will write the entire file out by using the WriteRecords method.  Note this method will also normally write the header out automatically.

We then write the records out again but this time we will do so by iterating thru the records collection and writing the entire record using the WriteRecord method and also by writing the fields of the record using the WriteField method.

One point to note that if you were writing a file using the WriteRecord or WriteField methods, you will not get a header record in the file.   If you wish to have a header record us the WriteHeader method (example is commented out in final code).

A completed example can be found below:

 

You can download this sample code with CsvHelper and CSV file here:

Reading CSV files using CSVHelper package (C#, IEnumerable)

I have come across many times the need to incorporate the reading of CSV files in an application.  Below I will show how to use Josh Close’s CsvHelper to  import a CSV file which looks like the following (file will be included below).

CommonName,FormalName,TelephoneCode,CountryCode
Argentina,Argentine Republic,54,ARG
Armenia,Republic of Armenia,374,ARM
Australia,Commonwealth of Australia,61,AUS
Austria,Republic of Austria,43,AUT
“Bahamas, The”,Commonwealth of The Bahamas,-241,BHS
Bangladesh,People’s Republic of Bangladesh,880,BGD

Note this CSV file is a simpler file based on the file which is located here.

The first thing to do is to Install CsvHelper.  To do this run the following command in the Package Manager Console:

Now add CsvHelper to the program by adding:

The next step is to create a class which has properties with the same name of the column headings found in the csv file.  Below you will find an example of a class which does this:

Finally create an instance of CSVReader and invoke the GetRecords method using the DataRecord class.  Below you will find an example of this:

An completed example can be found below:

You can download this sample code with  CsvHelper and CSV file here: CVSHelperReadSample

C# Methods with Unlimited Parameters

To create a method that can take unlimited parameters, utilize the params keyword in the method declaration.  This allows you to specify a method parameter that take an argument where the number of arguments is variable.   You are not allowed to have any other method parameters declared after a params method parameter.

MSDN resource: params

Example:

PSGMUMGGBJAR

%d bloggers like this: