Usage: How to Use Csv Classes
The CSV classes collection provides two classes – CsvReader, CsvWriter – which are used directly, and a CsvParseException which may be thrown while reading a CSV stream (in addition to the usual System.IO exceptions).
The classes move tabular data between an internal representation of an array of string arrays (String[][]) and a Stream (e.g. FileStream).
The following features are supported:
- Read CSV data from a Stream into an array of string arrays.
- Write CSV data to a Stream from an array of string arrays.
- Read or Write individual records to/from a string array.
- Read or Write individual Fields to/from a string.
This page presents some basic examples of how the classes can be used. For more examples, refer to the Test classes and sample applications also in the GitHub repository.
Read CSV Data From a Stream
CSV data may be read from a Stream – the following snippet of code shows how to read from a file:
String filename = "C:/PathToFile/sample.csv "; Stream stream = null; try { stream = File.OpenRead( filename ); CsvReader reader = new CsvReader( stream ); string[][] records = reader.ReadAll(); // Do something with the records… } catch( Exception ex ) { // Handle exception. } finally { if( stream != null ) { stream.Close(); } }
Write CSV Data To a Stream
Data may be written in CSV format to a Stream – the following snippet of code shows how to write data to a CSV file:
String filename = "C:/PathToFile/sample.csv "; Stream stream = null; String[][] records = …; try { stream = File.OpenWrite( filename ); CsvWriter writer = new CsvWriter( stream ); writer.WriteAll( records ); stream.Flush(); } catch( Exception ex ) { // Handle exception. } finally { if( stream != null ) { stream.Close(); } }
Read or Write A Record
To read or write a record of data, the following code may be used:
CsvReader reader = new CsvReader( stream ); String[] record = reader.ReadRecord(); //... CsvWriter writer = new CsvWriter( stream ); writer.WriteRecord( record );
Read or Write Individual Fields
To read or write an individual field, the following code may be used:
CsvReader reader = new CsvReader( stream ); String field = reader.ReadField(); // ... CsvWriter writer = new CsvWriter( stream ); writer.WriteField( field );
Options
When writing fields, the class checks if each field contains data that needs to be quoted. This behaviour is modified using the QuoteLimit value – initially set to 1000. Any string less than or equal to QuoteLimit is checked – any over that number is simply quoted anyway. Set it to 0 to quote all except empty fields.
CsvWriter writer = new CsvWriter( stream ); writer.QuoteLimit = 0; // Quote all except empty fields.
The separator used is, by default (and by definition) a comma (‘,’), but this may be changed by setting the value of Separator to the desired character.
CsvReader reader = new CsvReader( stream ); reader.Separator = '|'; // Use the vertical bar character as a separator. //... CsvWriter writer = new CsvWriter( stream ); writer.Separator = '|'; // Use the vertical bar character as a separator.