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.
public static void ToCSV<T>(this IEnumerable<T> collection, Stream stream, Encoding encoding, CultureInfo culture)
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)
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!):

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")
There are many other overloads to use when you don't know the type
public static IEnumerable<T> ReadCVS<T>(this Stream stream, Encoding encoding, CultureInfo culture, bool skipFirst) where T : new()
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()