Orc.Csv

NameBadge
ChatJoin the chat at https://gitter.im/WildGums/Orc.Csv
DownloadsNuGet downloads
Stable versionStable version
Unstable versionUnstable version
Find the source at https://github.com/WildGums/Orc.Csv.

Small super cool 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)));
    }
}

Contributions

We would like to thank the following contributors:

Want to contribute to the documentation? We have a guide for that!


Questions

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