Search code examples
c#csvhelper

CsvHelper : Adding a title using CsvHelper


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 table title before the records are written as mentioned below:

enter image description here

My old code without the title:

  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);
            csvWriter.WriteRecords(records);
            writer.Close();
        }

Old Csv:

enter image description here

My Current work around code:

            using (var writer = new StringWriter())
            using (var csvWriter = new CsvWriter(writer))
            {
                var ReportName = "Test Class";
                csvWriter.Configuration.RegisterClassMap(classMap);
                csvWriter.WriteRecords(records);
                writer.Close();
                return $"ReportName:, {ReportName},\n{csvWriter.Context.Writer.ToString()}";
            }

My Questions: 1) Is it possible with CsvHelper? 2) If so How?


Solution

  • 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