Full documentation is available in download section. Here are presented only short examples without any deeper explanations.

SimpleCSVWriter

With this class, You can create a CSV stream or file. To write a record to a file with header use the code below
using (SimpleCSVWriter writer = new SimpleCSVWriter(@".\simple.csv")) {
  writer.WriteHeader(new string[] { "A", "B", "C", "D" });   
  writer.WriteLine(new string[] { "1", "2", "3", "4" }); 
}

SimpleCSVReader

To read an existing CSV file use SimpleCSVReader class. If the file has a header, you can set HasHeader property to true - in this case, when You read a line from CSV, then the reader will read the header first. You can also manually read the header using ReadHeader(). To access a cell value in the record use the indexer. There are two types of indexer: numerical - accepts an integer (from 1) or string indexer, that accepts label of ther column header. Using string indexer is possible only when HasHeader was set to true or ReadHeader() was executed.
using (SimpleCSVReader reader = new SimpleCSVReader(@".\simple.csv")) {   
  reader.HasHeader = true;   
  while (reader.ReadLine()) { 
    // To read a column value use: reader[“ColumnName”]   
    // reader[1] is the same like reader["A"]
  } 
} 

Serializing

With SimpleCSV library is possible to serialize/deserialize object to/from a CSV file. To do this, You have to label properties of Your class with SimpleCSVAttribute like below
public class MyObject
{         
  [SimpleCSV]
  public decimal ID { get; set; }          

  [SimpleCSV(Label="Name")]
  public string Title { get; set; }
}


and then You have to build a SimpleCSVSerializer like below
using (SimpleCSVSerializer<MyObject> serializer = new SimpleCSVSerializer<MyObject>()) { 
  \\ serializing or deserializing
}

Serializing

To serialize an object to file use SimpleCSVWriter instance and Serialize method of SimpleCSVSerializer instance like below
  serializer.Serialize(writer, myObject);

Deserializing

To deserialize a record from CSV use DeserializeLine from the serializer with a SimpleCSVReader instance
  MyObject record;
  while (serializer.DeserializeLine(reader, out record)) {
    // operate on the record
  }

Last edited Jul 11, 2010 at 5:09 PM by D3M80L, version 4

Comments

No comments yet.