Answers for "csvwriter write header with space C#"

0

csvwriter write header with space C#

void Main()
{
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    using (var reader = new StreamReader(stream))
    using (var csv = new CsvWriter(writer))
    {
        var records = new List<Person>
        {
            new Test { DisplayName = "one", Age = 1},
            new Test { DisplayName = "two", Age = 2 },
        };
        
        csv.Configuration.RegisterClassMap<PersonMap>();
        csv.WriteRecords(records);
        
        writer.Flush();
        stream.Position = 0;
        
        Console.WriteLine(reader.ReadToEnd());
    }
}

public class Person
{
    public string DisplayName { get; set; }
    public int Age { get; set; }
}

public sealed class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Map(m => m.DisplayName).Name("Display Name");
        Map(m => m.Age);
    }
}
Posted by: Guest on July-02-2021

Browse Popular Code Answers by Language