CsvHelper : Adding a title using CsvHelper

后端 未结 3 2036
醉酒成梦
醉酒成梦 2021-01-15 09:46

I am using CsvHelper to convert dapper objects to CsvFiles. I am using classmaps to map properties for indices and name mapping. The issue is I need a row with the t

相关标签:
3条回答
  • 2021-01-15 10:11

    You can write fields and rows by hand.

    void Main()
    {
        var records = new List<Foo>
        {
            new Foo { Id = 1, Name = "one" },
            new Foo { Id = 2, Name = "two" },
        };
        using (var writer = new StringWriter())
        using (var csv = new CsvWriter(writer))
        {
            csv.Configuration.RegisterClassMap<FooMap>();
    
            csv.WriteField("Title:");
            csv.WriteField("Title");
            csv.NextRecord();
    
            csv.WriteRecords(records);
            writer.ToString().Dump();
        }
    }
    
    public class Foo
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class FooMap : ClassMap<Foo>
    {
        public FooMap()
        {
            Map(m => m.Id).Index(0).Name("S.No.");
            Map(m => m.Name).Index(1);
        }
    }
    

    Output:

    Title:,Title
    S.No.,Name
    1,one
    2,two
    
    0 讨论(0)
  • 2021-01-15 10:25

    CSV files have no titles. The question describes a flat text report, not a CSV file. CsvHelper, as the name implies, is a helper library that writes CSVs, it's not a full featured report generator.

    The library allows other code to write arbitrary text before or after it finished though, as it works on top of an abstract TextWriter instead of a stream or file. One could even write extra text between records if required.

    You can use the writer you already have to write whatever you want before or after the call to csvWriter.WriteRecords(records);, eg :

    using (var writer = new StreamWriter(@"C:\Users\NPandian\Desktop\test.csv", false, System.Text.Encoding.UTF8))
    using (var csvWriter = new CsvWriter(writer))
    {
        var ReportName = "Test Class";
        csvWriter.Configuration.RegisterClassMap(classMap);
    
        //Doesn't matter where this is called as long as it is before `WriteRecords`
        writer.WriteLine($"ReportName:, {ReportName}");
    
        csvWriter.WriteRecords(records);
    
        //No need to explicitly close, that's what `using` is for
    }
    

    CsvWriter accepts any TextWriter object and just writes its data to that writer. It doesn't try to modify it in any other way. It won't affect any other text already written to that TextWriter

    0 讨论(0)
  • 2021-01-15 10:32

    For CSVHelper version 15.0.0 and above use:

    void Main()
    {
        var records = new List<Foo>
        {
            new Foo { Id = 1, Name = "one" },
            new Foo { Id = 2, Name = "two" },
        };
        using (var writer = new StringWriter())
        using (var csv = new CsvWriter(writer, CultureInfo.CurrentCulture))
        {
            csv.Configuration.RegisterClassMap<FooMap>();
    
            csv.WriteField("Title:");
            csv.WriteField("Title");
            csv.NextRecord();
    
            csv.WriteRecords(records);
            writer.ToString().Dump();
        }
    }
    
    public class Foo
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class FooMap : ClassMap<Foo>
    {
        public FooMap()
        {
            Map(m => m.Id).Index(0).Name("S.No.");
            Map(m => m.Name).Index(1);
        }
    }
    
    0 讨论(0)
提交回复
热议问题