Click here to Skip to main content
Licence MIT
First Posted 22 Jun 2010
Views 47,451
Downloads 3,935
Bookmarked 58 times

C# CSV Reader and Writer

By | 22 Jun 2010 | Article
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

About the Author

CroweMan

Web Developer

United Kingdom United Kingdom

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow do I use CsvWrite to flush each row in a csv file PinmemberMember 827871311:14 9 Dec '11  
Bugbug with empty feild Pinmembernoav0:39 6 Dec '11  
BugRe: bug with empty feild Pinmemberizogi15:56 23 May '12  
GeneralMy vote of 5 PinmemberMember 79074903:30 28 Aug '11  
Generalhere we go with a secound solution Pinmemberoscar@angress.de9:50 23 Feb '11  
GeneralStyleCopped Version of this Project PinmemberTatworth10:23 21 Feb '11  
GeneralMy vote of 5 PinmemberTatworth0:09 16 Feb '11  
GeneralThanks! PinmemberD-Three2:36 2 Nov '10  
QuestionDid you miss this one? PinmemberIceCreamWizard1:46 29 Jun '10  
AnswerRe: Did you miss this one? PinmemberCroweMan3:43 29 Jun '10  
AnswerRe: Did you miss this one? PinmemberAndrew Rissing4:31 29 Jun '10  
GeneralMy vote of 2 PinmemberMarc Scheuner0:14 25 Jun '10  
GeneralA long way to go PinmvpJosh Fischer3:15 23 Jun '10  
GeneralRe: A long way to go Pinmemberflyingxu3:33 13 Dec '11  
GeneralNot enough code and explanation PinmvpPIEBALDconsult18:03 22 Jun '10  
GeneralCSV Helpers PinmemberNarshe7:23 22 Jun '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 22 Jun 2010
Article Copyright 2010 by CroweMan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid