CsvEditSharp is a CSV editor. You can describe reading and writing settings in CSharp script.
C#
Latest commit a8c5486 Jan 8, 2017 @pierre3 committed on GitHub Update README.md
Permalink
Failed to load latest commit information.
CsvEditSharp Change dialog startup location Dec 22, 2016
Documents Update image Dec 23, 2016
.gitattributes initial commit Nov 7, 2016
.gitignore initial commit Nov 7, 2016
CsvEditSharp.sln Fix typo solution name Nov 26, 2016
LICENSE Initial commit Nov 7, 2016
README.md Update README.md Jan 8, 2017

README.md

CsvEditSharp

CsvEditSharp is a CSV editor. You can describe reading and writing settings in CSharp script.

CsvEditSharpMain03

Download

Framework

  • Windows Presentation Foundation (WPF)
  • .Net Framework 4.6.1

Liblary

Exsamples

sample csv

Name,Birthday,Gender,Married,PocketMoney
Joe Maldonado,6/4/1955,Male,N,$7028.74
Mae Ross,10/10/1981,Male,Y,$1847.49
Adele Owen,10/8/1993,Female,N,$2615.28
Nina Pope,7/24/1967,Male,Y,$6265.55
Marian Young,12/3/1969,Male,N,$9165.00
Nicholas Cruz,3/16/1971,Male,Y,$8435.71
Effie Moody,7/23/1990,Female,Y,$6883.24
Larry Carson,11/12/1970,Male,N,$723.88
Edna Watson,3/6/1967,Male,Y,$203.56
Bettie Bishop,9/17/1985,Female,N,$5471.29
Verna Fowler,12/8/1972,Male,N,$43.31
Ola Zimmerman,6/5/1961,Female,Y,$1354.57
Rachel Hart,10/3/1975,Female,Y,$8221.86
...

configuration script

/* CSV Text Encoding */
Encoding = Encoding.GetEncoding("utf-8");

/* Class Declarations */
class Person
{
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
    public Gender Gender { get; set; }
    public bool Married { get; set; }
    public double PocketMoney { get; set; }
}

enum Gender
{
    Male,
    Female
}

/* Set class map settings via CsvHelper */
RegisterClassMap<Person>(classMap =>
{
    classMap.Map(m => m.Name).Name("Name");
    classMap.Map(m => m.Birthday).Name("Birthday")
        .TypeConverterOption("M/d/yyyy");
    classMap.Map(m => m.Gender).Name("Gender");
    classMap.Map(m => m.Married).Name("Married")
        .TypeConverterOption(true,"Y")
        .TypeConverterOption(false,"N");

    var culcure = System.Globalization.CultureInfo.GetCultureInfo("en-us");
    classMap.Map(m => m.PocketMoney).Name("PocketMoney")
        .TypeConverterOption("C")
        .TypeConverterOption(NumberStyles.Currency)
        .TypeConverterOption(culcure);
});

CsvEditSharpMain02

Script API

Encoding Property

Encoding Encoding { get; set; }

Sets a System.Text.Encoding object for reading a CSV file.

Encoding = Encoding.GetEncoding("utf-8");

RegisterClassMap Method

void RegisterClassMap<T>();
void RegisterClassMap<T>(Action<CsvClassMap<T>> propertyMapSetter);
void RegisterClassMap<T>(Action<CsvClassMap<T>> propertyMapSetter, RegisterClassMapTarget target);

Set class map settings, using CsvHelper.Configuration.CsvClassMap.

/* Set default class map settings */
RegisterClassMap<Person>();

/* Set class map settings for CSV reader and writer */
RegisterClassMap<Person>(classMap => {
    classMap.Map(m => m.Name);
    classMap.Map(m => m.Birthday);
    classMap.Map(m => m.Gender);
    classMap.Map(m => m.Married)
        .TypeConverterOption(true,"Y")
        .TypeConverterOption(false,"N");
    classMap.Map(m => m.PocketMoney)
        .TypeConverterOption("C")
        .TypeConverterOption(NumberStyles.Currency);
});

/* Set class map settings for CSV reader only */
RegisterClassMap<Person>(classMap => {
    classMap.Map(m => m.Name);
    classMap.Map(m => m.Birthday);
    classMap.Map(m => m.Gender);
    classMap.Map(m => m.Married)
        .TypeConverterOption(true,"Y")
        .TypeConverterOption(false,"N");
    classMap.Map(m => m.PocketMoney)
        .TypeConverterOption("C")
        .TypeConverterOption(NumberStyles.Currency);
}, RegisterClassMapTarget.Reader);

SetConfiguration Method

void SetConfiguration(Action<CsvConfiguration> configurationSetter);

Sets a configuration for CSV reader and writer, using CsvHelper.Configuration.CsvConfiguration

(sample.csv)

Joe Maldonado;6/4/1955;Male;N;$7028.74
# comment line
Mae Ross;10/10/1981;Male;Y;$1847.49
Adele Owen;10/8/1993;Female;N;$2615.28
Nina Pope;7/24/1967;Male;Y;$6265.55
Marian Young;12/3/1969;Male;N;$9165.00
SetConfiguration(config =>
{
    config.HasHeaderRecord = false;
    config.AllowComments = true;
    config.Comment = '#';
    config.Delimiter = ';';
    //etc...
});

AddValidation Method

void AddValidation<TType, TMember>(Expression<Func<TType, TMember>> memberSelector, Func<TMember, bool> validation, string errorMessage);

Sets a validation in column.

AddValidation<Person,DateTime>(
    m => m.Birthday , 
    dt => dt <= DateTime.Now.Date,
    "Cannot enter a future date.");

AddValidation<Person, double>(
    m => m.PocketMoney , 
    n => (n > 0) && (n < 10000.0),
    "PocketMoney must be in the range $0 to $10000.");

validation01
validation02

Query Method

void Query<T>(Func<IEnumerable<T>, IEnumerable<T>> query);
void Query<T>(Action<IEnumerable<T>> query);

Filter & Sort Data

Query<Person>(source => source
    .Where(m => m.Gender == Gender.Female )
    .Where(m => m.Married )
    .OrderBy(m => m.PocketMoney) );

query

Update Data

Query<Person>( record => record
    .Where( m => m.Gender == Gender.Male )
    .Where( m => m.Married )
    .ForEach( m =>
    {
        m.Name += " *";
        m.PocketMoney = 0;
    })
);

foreach