CsvHelper : Adding a title using CsvHelper

心已入冬 提交于 2020-05-08 16:02:01

问题


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:

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:

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?


回答1:


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



回答2:


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




回答3:


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);
    }
}


来源:https://stackoverflow.com/questions/56238544/csvhelper-adding-a-title-using-csvhelper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!