Orc.CsvHelper

Name Badge
Chat Join the chat at https://gitter.im/WildGums/Orc.CsvHelper
Downloads NuGet downloads
Stable version Version
Unstable version Pre-release version

Find the source at https://github.com/WildGums/Orc.CsvHelper

Small library of extensions and helper methods for the CsvHelper library.

Features

/// <summary>
/// Create CSharp files to consume CSV files.
/// A standard POCO cs file as well as the CsvHelper Mapping cs file will be created.
/// All properties in the POCO will be of type string. So please update accordingly.
/// </summary>
public static class CodeGeneration
{
    public static void CreateCSharpFilesForAllCsvFiles(string inputFoler, string namespaceName, string outputFolder)
    {
        var csvFiles = GetCsvFiles(inputFoler);

        foreach (var csvFile in csvFiles)
        {
            CreateCSharpFiles(csvFile, namespaceName, outputFolder);
        }
    }
    ...
}
public static IEnumerable<T> ReadCsv<T>(string csvFilePath, Action<T> initializer = null, Type mapType = null, CsvConfiguration csvConfiguration = null, bool throwOnError = false)

or

public static IEnumerable<T> ReadCsv<T>(string csvFilePath, CsvClassMap map, Action<T> initializer = null, CsvConfiguration csvConfiguration = null, bool throwOnError = false)

Example:

var records = CsvWriterHelper.ReadCsv<MyClass>(scvFilePath, MyClassMap);
public static void WriteCsv<TRecord, TMap>(IEnumerable<TRecord> records, string csvFilePath, CsvConfiguration csvConfiguration = null, bool throwOnError = false)
public static void ToCsv<TRecord>(this IEnumerable<TRecord> records, string csvFilePath, Type csvMap = null, CsvConfiguration csvConfiguration = null, bool throwOnError = false)

Example:

records.ToCsv<MyClass>(csvFilePath, typeof(MyClassMap));

Converters

public class EmployeeMap : CsvClassMap<Employee>
{
    public EmployeeMap()
    {
        Map(x => x.Name).Name("Name");
        Map(x => x.StartDate).Name("StartDate");

        // Parse the enum. Use EmployeeRate.PerHour on failure.
        Map(x => x.Rate).Name("Rate")
            .TypeConverter(new EnumConverter<EmployeeRate>(EmployeeRate.PerHour));

        // Parse nullable DateTime value
        Map(x => x.DischargeDate).Name("DischargeDate");
            .TypeConverter(new NullableDateTimeConverter());

        // Parse Yes and No string values as booleans
        Map(x => x.Married).Name("Married");
            .TypeConverter(new YesNoToBooleanConverter());

        Map(x => x.WorkDayDuration).Name("WorkDayDuration")
            .TypeConverter(new TypeConverter<TimeSpan>((hours) => return new TimeSpan(Convert.ToDouble(hours), 0, 0)));
    }
}

Have a question about Catel or WildGums controls? Use StackOverflow with the Catel tag!


Discussion