📜  csvwriter 用空格 C# 写入标头 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:52.584000             🧑  作者: Mango

代码示例1
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
        {
            new Test { DisplayName = "one", Age = 1},
            new Test { DisplayName = "two", Age = 2 },
        };
        
        csv.Configuration.RegisterClassMap();
        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
{
    public PersonMap()
    {
        Map(m => m.DisplayName).Name("Display Name");
        Map(m => m.Age);
    }
}