BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

Creating CSV Files

Comma-separated values (CSV) files can be used to transfer information between systems that support no other common interface. CSV files can be read using standard functionality from the .NET framework but creating them requires a custom class.

Writing Comments

For our final method we'll add the code that allows a comment to be written to the file. First we write the comment token, using the Write method so that no carriage return or line feed is added. We then use WriteLine to add the text of the comment and complete the line in the CSV file.

public void WriteComment(string comment)
{
    Write(_commentToken);
    WriteLine(comment);
}

Testing the CsvWriter Class

To test the class we need to create several string arrays to write to a file. We can then create a new writer and store a comment and the data. The code below, which should be added to the Main method of a console application, shows this. Note the use of the using statement to ensure that the CsvWriter is correctly disposed and the file resource is released. NB: You should change the path passed to the CsvWriter constructor to one that is suitable for your system.

string[] headers = new string[] { "Company", "Spend", "Rating" };
string[] company1 = new string[] { "Quality Foods Limited", "1000000", "Gold" };
string[] company2 = new string[] { "The Pieman", "50000", "Silver" };
string[] company3 = new string[] { "Bill's Fruit and Veg", "10000", "Bronze" };

using (CsvWriter writer = new CsvWriter("d:\\test.txt"))
{
    writer.WriteComment("File created on " + DateTime.Now.ToString("d MMMM yyyy"));
    writer.WriteLine(headers);
    writer.WriteLine(company1);
    writer.WriteLine(company2);
    writer.WriteLine(company3);
}

/* FILE CONTENT

## File created on 9 December 2012
Company,Spend,Rating
Quality Foods Limited,1000000,Gold
The Pieman,50000,Silver
Bill's Fruit and Veg,10000,Bronze

*/

The above example doesn't process any special characters so the result is a clean CSV file with no items surrounded by quotes. In the next example I have changed the delimiter and quote characters. Now that the quote character is an apostrophe and the final array's data includes such a character, the first item in the last CSV row must be escaped and quoted.

string[] headers = new string[] { "Company", "Spend", "Rating" };
string[] company1 = new string[] { "Quality Foods Limited", "1000000", "Gold" };
string[] company2 = new string[] { "The Pieman", "50000", "Silver" };
string[] company3 = new string[] { "Bill's Fruit and Veg", "10000", "Bronze" };

CsvWriter writer = new CsvWriter("d:\\test.txt");
writer.Delimiter = '|';
writer.Quote = '\'';

using (CsvWriter writer = new CsvWriter("d:\\test.txt"))
{
    writer.WriteComment("File created on " + DateTime.Now.ToString("d MMMM yyyy"));
    writer.WriteLine(headers);
    writer.WriteLine(company1);
    writer.WriteLine(company2);
    writer.WriteLine(company3);
}

/* FILE CONTENT

## File created on 9 December 2012
Company|Spend|Rating
Quality Foods Limited|1000000|Gold
The Pieman|50000|Silver
'Bill''s Fruit and Veg'|10000|Bronze

*/
9 December 2012