Click here to Skip to main content
15,881,715 members
Articles / Web Development / ASP.NET

C# CSV Reader and Writer

Rate me:
Please Sign up or sign in to vote.
4.86/5 (16 votes)
22 Jun 2010MIT1 min read 224.9K   14.4K   89   21
Class library which provides the ability to read and write CSV files

Introduction

I have lost count of the number of projects I have worked on that required me to write data in a CSV format and present it to the user for download. On a recent project, I decided that it would be a good idea to implement some classes that would simplify the process of reading from and writing to a CSV file.

I had a scan around on the web to find a simple solution to the problem. There were many classes that already do this, but I found them way too complex.

So I decided to build my own!

Using the Code

Below are a few samples of how the classes can be used. The classes have many constructor and method overloads that allow reading/writing from/to files, strings and streams.

Using the CsvReader Class

C#
List<List<string>> records = new List<List<string>>();

using (CsvReader reader = new CsvReader(FilePath, Encoding.Default))
{
    while (reader.ReadNextRecord())
        records.Add(reader.Fields);
} 

The CsvReader class has the following properties:

  • TrimColumns - Gets or sets whether column and heading values should be trimmed
  • HasHeaderRow - Gets or sets whether the CSV content has a header row
  • Fields - Gets the fields in the current Record
  • FieldCount - Gets the number of fields in the current record

Using the CsvFile Class

C#
CsvFile file = new CsvFile();
file.Populate(FilePath, true);

The CsvFile class has the following properties:

  • Headers - Gets the column headers
  • Records - Gets the records within the CSV file
  • HeaderCount - Gets the number of header columns
  • RecordsCount - Gets the number of records within the CSV file

Using the CsvWriter Class

C#
CsvFile csvFile = new CsvFile();
csvFile.Populate(FilePath, true);

using (CsvWriter writer = new CsvWriter())
{
   writer.WriteCsv(csvFile, FilePath);
}

The CsvWriter class has the following properties:

  • ReplaceCarriageReturnsAndLineFeedsFromFieldValues - Gets or sets whether carriage returns and line feeds should be replaced in field values.
  • CarriageReturnAndLineFeedReplacement - Gets or sets the character replacement for carriage returns and line feeds.

History

Version 1.0 (22-06-2010)

  • 1.0: First release

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Web Developer
United Kingdom United Kingdom
I have been developing web and software applications for 9 years originally in Microsoft Visual Fox Pro. But for the last 8 years I have been using Microsoft.Net.

I love writing code and especially enjoy writing tools to aid development.

I have developed my own code generator which generates C# code from a SQL Server database. The generator generates Business Components, Data Components and entities and is optimised for performance as it uses stored procedures and SqlDataReaders.

FrameworkGen can be downloaded for free from http://www.elencysolutions.co.uk.

Comments and Discussions

 
QuestionElegant and simple. Pin
me@vbman.com22-Apr-20 10:37
me@vbman.com22-Apr-20 10:37 
QuestionGreat! Pin
tolstyi.toxa13-Dec-13 21:45
tolstyi.toxa13-Dec-13 21:45 
SuggestionReadNextRecord Pin
Alexander Kolenov22-Jun-12 1:30
Alexander Kolenov22-Jun-12 1:30 
QuestionHow do I use CsvWrite to flush each row in a csv file Pin
Member 82787139-Dec-11 11:14
Member 82787139-Dec-11 11:14 
Bugbug with empty feild Pin
noav6-Dec-11 0:39
noav6-Dec-11 0:39 
BugRe: bug with empty feild Pin
izogi23-May-12 15:56
izogi23-May-12 15:56 
GeneralRe: bug with empty feild Pin
jemnet28-Jan-13 8:06
jemnet28-Jan-13 8:06 
GeneralRe: bug with empty feild Pin
Member 1013498421-Jul-14 10:17
Member 1013498421-Jul-14 10:17 
GeneralMy vote of 5 Pin
Member 790749028-Aug-11 3:30
Member 790749028-Aug-11 3:30 
Generalhere we go with a secound solution Pin
oscar@angress.de23-Feb-11 9:50
oscar@angress.de23-Feb-11 9:50 
GeneralStyleCopped Version of this Project Pin
Tatworth21-Feb-11 10:23
Tatworth21-Feb-11 10:23 
GeneralMy vote of 5 Pin
Tatworth16-Feb-11 0:09
Tatworth16-Feb-11 0:09 
GeneralThanks! Pin
D-Three2-Nov-10 2:36
D-Three2-Nov-10 2:36 
QuestionDid you miss this one? Pin
IceCreamWizard29-Jun-10 1:46
IceCreamWizard29-Jun-10 1:46 
AnswerRe: Did you miss this one? Pin
CroweMan29-Jun-10 3:43
CroweMan29-Jun-10 3:43 
AnswerRe: Did you miss this one? Pin
Andrew Rissing29-Jun-10 4:31
Andrew Rissing29-Jun-10 4:31 
GeneralMy vote of 2 Pin
Marc Scheuner25-Jun-10 0:14
professionalMarc Scheuner25-Jun-10 0:14 
GeneralA long way to go PinPopular
Josh Fischer23-Jun-10 3:15
Josh Fischer23-Jun-10 3:15 
GeneralRe: A long way to go Pin
flyingxu13-Dec-11 3:33
flyingxu13-Dec-11 3:33 
GeneralNot enough code and explanation PinPopular
PIEBALDconsult22-Jun-10 18:03
mvePIEBALDconsult22-Jun-10 18:03 
GeneralCSV Helpers Pin
Narshe22-Jun-10 7:23
Narshe22-Jun-10 7:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.