Click here to Skip to main content
12,755,516 members (40,746 online)
Click here to Skip to main content
Add your own
alternative version

Stats

261.2K views
11.8K downloads
87 bookmarked
Posted 22 Jun 2010

C# CSV Reader and Writer

, 22 Jun 2010 MIT
Rate this:
Please Sign up or sign in to vote.
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

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

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

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

Share

About the Author

CroweMan
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.

You may also be interested in...

Comments and Discussions

 
QuestionGreat! Pin
tolstyi.toxa13-Dec-13 22:45
membertolstyi.toxa13-Dec-13 22:45 
SuggestionReadNextRecord Pin
Alexander Kolenov22-Jun-12 2:30
memberAlexander Kolenov22-Jun-12 2:30 
QuestionHow do I use CsvWrite to flush each row in a csv file Pin
Member 82787139-Dec-11 12:14
memberMember 82787139-Dec-11 12:14 
Bugbug with empty feild Pin
noav6-Dec-11 1:39
membernoav6-Dec-11 1:39 
BugRe: bug with empty feild Pin
izogi23-May-12 16:56
memberizogi23-May-12 16:56 
GeneralRe: bug with empty feild Pin
jemnet28-Jan-13 9:06
memberjemnet28-Jan-13 9:06 
GeneralRe: bug with empty feild Pin
Member 1013498421-Jul-14 11:17
memberMember 1013498421-Jul-14 11:17 
GeneralMy vote of 5 Pin
Member 790749028-Aug-11 4:30
memberMember 790749028-Aug-11 4:30 
Generalhere we go with a secound solution Pin
oscar@angress.de23-Feb-11 10:50
memberoscar@angress.de23-Feb-11 10:50 
GeneralStyleCopped Version of this Project Pin
Tatworth21-Feb-11 11:23
memberTatworth21-Feb-11 11:23 
GeneralMy vote of 5 Pin
Tatworth16-Feb-11 1:09
memberTatworth16-Feb-11 1:09 
GeneralThanks! Pin
D-Three2-Nov-10 3:36
memberD-Three2-Nov-10 3:36 
QuestionDid you miss this one? Pin
IceCreamWizard29-Jun-10 2:46
memberIceCreamWizard29-Jun-10 2:46 
AnswerRe: Did you miss this one? Pin
CroweMan29-Jun-10 4:43
memberCroweMan29-Jun-10 4:43 
AnswerRe: Did you miss this one? Pin
Andrew Rissing29-Jun-10 5:31
memberAndrew Rissing29-Jun-10 5:31 
GeneralMy vote of 2 Pin
Marc Scheuner25-Jun-10 1:14
memberMarc Scheuner25-Jun-10 1:14 
GeneralA long way to go Pin
Josh Fischer23-Jun-10 4:15
mvpJosh Fischer23-Jun-10 4:15 
GeneralRe: A long way to go Pin
flyingxu13-Dec-11 4:33
memberflyingxu13-Dec-11 4:33 
GeneralNot enough code and explanation Pin
PIEBALDconsult22-Jun-10 19:03
mvpPIEBALDconsult22-Jun-10 19:03 
GeneralCSV Helpers Pin
Narshe22-Jun-10 8:23
memberNarshe22-Jun-10 8: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 22 Jun 2010
Article Copyright 2010 by CroweMan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid