Orc.Csv
Name | Badge |
---|---|
Chat | |
Downloads | |
Stable version | |
Unstable version |
Small super cool library of extensions and helper methods for the CsvHelper library.
- Static methods are provided to read or write to a csv file with one line of code.
- Sensible configuration options are provided out of the box (but can be overwritten if needed).
- Reading from a csv file will not lock it.
- Capture a meaningful error message when an exception is thrown while reading a csv file.
Features
Code generation
=> Use static classCodeGeneration
for generating C# POCO classes and their associated maps.
/// <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);
}
}
...
}
CsvReader helper
=> Use static classCsvReaderHelper
for reading csv files with a single line of code.
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);
CsvWriter helper
=> Use static classCsvWriterHelper
for writing records into a csv file.
public static void WriteCsv<TRecord, TMap>(IEnumerable<TRecord> records, string csvFilePath, CsvConfiguration csvConfiguration = null, bool throwOnError = false)
CsvExtensions
=> for writing object collections into a csv file using theToCsv()
method.
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
EnumConverter
- generic string to enum converterStringToNullableDateTimeConverter
- converts string to DateTime? typeYesNoToBooleanConverter
- converts “yes” and “no” strings to true and false values correspondinglyTypeConverter
- generic type converter which in some cases is more fluent than the default one
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!