CsvHelper:使用CsvHelper添加标题

时间:2019-05-21 12:41:06

标签: c# csvhelper

我正在使用CsvHelper将dapper对象转换为CsvFiles。我正在使用类映射来映射索引和名称映射的属性。问题是在记录如下所述之前,我需要具有表标题的行

enter image description here

我没有标题的旧代码:

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

旧Csv:

enter image description here

我当前的代码变通方法:

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

我的问题: 1)CsvHelper是否可能? 2)如果可以,怎么办?

3 个答案:

答案 0 :(得分:2)

您可以手动编写字段和行。

test<-df%>%
  mutate(var_t1_lag1= ifelse(lag(return, n= lag_day[1] )>threshold[1],return, NA))%>%
  mutate(var_t2_lag1= ifelse(lag(return, n= lag_day[1] )>threshold[2],return, NA))%>%
  mutate(var_t3_lag1= ifelse(lag(return, n= lag_day[1] )>threshold[3],return, NA))%>%
  mutate(var_t1_lag2= ifelse(lag(return, n= lag_day[2] )>threshold[1],return, NA))%>%
  mutate(var_t2_lag2= ifelse(lag(return, n= lag_day[2] )>threshold[2],return, NA))%>%
  mutate(var_t3_lag2= ifelse(lag(return, n= lag_day[2] )>threshold[3],return, NA))%>%
  mutate(var_t1_lag3= ifelse(lag(return, n= lag_day[3] )>threshold[1],return, NA))%>%
  mutate(var_t2_lag3= ifelse(lag(return, n= lag_day[3] )>threshold[2],return, NA))%>%
  mutate(var_t3_lag3= ifelse(lag(return, n= lag_day[3] )>threshold[3],return, NA))

输出:

df <- tibble(
  date= today()+0:12,
  return=c(1,2.5,2,3,5,6.5,1,9,3,2,4,7,2)
)

答案 1 :(得分:1)

CSV文件没有标题。该问题描述的是纯文本报告,而不是CSV文件。顾名思义,CsvHelper是一个编写CSV的帮助程序库,它不是功能齐全的报告生成器。

该库允许其他代码在完成之前或之后写入任意文本,因为它可以在抽象TextWriter之上而不是流或文件上运行。如果需要,甚至可以在记录之间写额外的文本。

您可以使用writer来编写在调用csvWriter.WriteRecords(records);之前或之后想要的任何内容,例如:

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接受任何TextWriter对象,并将其数据写入该写入器。它不会尝试以任何其他方式对其进行修改。它不会影响已经写入该TextWriter

的任何其他文本

答案 2 :(得分:0)

对于 CSVHelper 版本 15.0.0 及更高版本,使用:

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