Signum Framework Logo
"Open framework that encourages convention over configuration, using C# code,
not XML files, to model at the right level of abstraction and achieve deadlines.
...but also has a full Linq provider, and syncs the schema for you!"
Login
RSS

Search

»



Main
Index
Map
Videos
Download
Source code
Tutorials
Forum
FAQ



Image



PoweredBy

Signum.Utilities.CSV

CSV is a very simple file format for tabular data with no format. See CSV in Wikipedia. Popular applications like Microsoft Excel can easily open and save files in this format.

CSV files really shine as a way to generate tabular logs and integrate data from other sources on your Loading Application, so we decided to make a very lightweight CSV library in Signum.Utilities.

For the sake of simplicity, the available options are limited:

  • Encoding: Used for reading and writing the files. The default Encoding is UTF8 but Excel usually use (in western countries) the Western European (Windows) - Codepage 1252 Encoding (Encoding.GetEncoding(1252)). You can see what is the current Encoding opening the CSV file in Visual Studio and using Advanced Save Options.

  • CultureInfo: Used for ToString and Parse (DateTime, Numbers...) CultureInfo.CurrentCulture is the default.

This library has two main functionalities, writing and reading CSV files.

Writing CSV Files

ToCSV method, as other utility methods here, uses public properties and fields to define the columns.

//General overload that takes an stream.
public static void ToCSV<T>(this IEnumerable<T> collection, Stream stream, Encoding encoding, CultureInfo culture)

//Overloads that takes a byte[], using a MemoryStream. Useful for storing in the database or sending to server/client. public static byte[] ToCSV<T>(this IEnumerable<T> collection) public static byte[] ToCSV<T>(this IEnumerable<T> collection, Encoding encoding) public static byte[] ToCSV<T>(this IEnumerable<T> collection, Encoding encoding, CultureInfo culture)

//Overloads that takes a filename, using a FileStream to write the CSV. //Result: the fileName (to allow chaining) public static string ToCSV<T>(this IEnumerable<T> collection, string fileName) public static string ToCSV<T>(this IEnumerable<T> collection, string fileName, Encoding encoding) public static string ToCSV<T>(this IEnumerable<T> collection, string fileName, Encoding encoding, CultureInfo culture)

Using a code very similar to the last example, but using ToCSV with a filename instead:

new DirectoryInfo(@"C:\Users\Public\Pictures\Sample Pictures").GetFiles()
                .Select(a=>new 
                 {
                   a.Name,  
                   Size = a.Length.ToComputerSize(), 
                   a.LastWriteTime, 
                   a.CreationTime 
                 })
                .ToCSV("myFile.csv"); 

We will get a simple CSV file that will look like this win Excel 2007 (Spanish Version!):

Image

Read CSV Files

Reading CSV files is a bit different. In order to get a IEnumerable that is filled with your CSV data you need a datasource (the file), and the type (T in this case) that is used as a template to fill the data.

So the first thing is to define a type that allows us to read the file:

public class CSVFileLine
{
   public string Name; 
   public long Size; 
   public DateTime LastWriteTime; 
   public DateTime CreationTime; 
}

Then we just read the file like this:

CSV.ReadCVS<CSVFileLine>("myFile.csv")

Take into account that the order each field/property is declared, not the name, is used to parse each line of the CSV file.

There are many other overloads to use when you don't know the type

//General method that Parses a CSV file from a string IEnumerable
//skipFirstLine: True to avoid reading the first line (headers)
//IMPORTANT: Returns an IEnumerable and uses deferred execution, it's your responsability to keep the Stream open.
public static IEnumerable<T> ReadCVS<T>(this Stream stream, Encoding encoding, CultureInfo culture, bool skipFirst) where T : new()

//Usefull overloads that completely read fileName file, and return the results in a List. //Default skipFirstLine is true public static List<T> ReadCVS<T>(string fileName) where T : new() public static List<T> ReadCVS<T>(string fileName, Encoding encoding) where T : new() public static List<T> ReadCVS<T>(string fileName, Encoding encoding, CultureInfo culture) where T : new() public static List<T> ReadCVS<T>(string fileName, Encoding encoding, CultureInfo culture, bool skipFirtsLine) where T : new()
Creative Commons License Signum Framework Site by Signum Software is licensed under a Creative Commons Attribution 3.0 License.
Powered by ScrewTurn Wiki version 3.0.5.600.