将数据写入CSV时如何排除标题

时间:2019-03-25 06:51:20

标签: c# winforms csvhelper

我正在将数据从public class写入CSV文件。当我要追加数据时,我想排除标题的导入,而仅从类中导入数据。我下面的代码同时导入标头和数据。希望得到帮助。谢谢。

Record.cs-我的班级

public class Record
{
    public string Name
    {
        get; set;
    }

    public DateTime DateOfBirth
    {
        get; set;
    }
}

Form1.cs-我的表单

public partial class Form1 : Form
{
    private List<Record> records;

    public Form1()
    {
        InitializeComponent();
        records = new List<Record>();
    }

    private void Savetocsv_Click(object sender, EventArgs e)
    {
        var myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        using (var writer = new StreamWriter(myDocument + "/my-data.csv", append: true))
        {
            using (var csv = new CsvWriter(writer))
            {
                csv.WriteRecords(records);
            }                
        }
    }

5 个答案:

答案 0 :(得分:2)

使用Configuration,您可以使用属性HasHeaderRecord

  

HasHeaderRecord:   获取或设置一个值,该值指示CSV文件是否具有标题记录。
  默认值为true。

var records = new List<Foo>
{
    new Foo { Id = 1, Name = "one" },
    new Foo { Id = 1, Name = "one" },
};

using (var writer = new StreamWriter($"file.csv"))
using (var csv = new CsvWriter(writer, new Configuration { HasHeaderRecord = false }))
{
    csv.WriteRecords(records);
}

结果文件:“ file.csv”

  

1;一个
  1;一个

或者简单地循环记录并将其写入:

var records = new List<Foo>
{
    new Foo { Id = 1, Name = "one" },
    new Foo { Id = 1, Name = "one" }
};

using (var writer = new StreamWriter($"file.csv"))
using (var csv = new CsvWriter(writer))
{
    foreach (var record in records)
    {
        csv.WriteRecord(record);
        csv.NextRecord();
    }
}

答案 1 :(得分:1)

按如下所示更改您的书写方式,然后CsvHelper.WriterConfiguration完成技巧(注意 HasHeaderRecord ):

private void Savetocsv_Click(object sender, EventArgs e)
{
    var myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    using (var writer = new StreamWriter(myDocument + "/my-data.csv", append: true))
    {
        using (var csv = new CsvWriter(writer, new Configuration { HasHeaderRecord = false }))
        {
            csv.WriteRecords(records);
        }                
    }
}

答案 2 :(得分:1)

配置类的名称已更改。

using (var csv = new CsvWriter(outputStream, new CsvConfiguration(CultureInfo.InvariantCulture)
{
    HasHeaderRecord = false
}))

答案 3 :(得分:0)

在调用之前从records中删除第一行:

csv.WriteRecords(records);

(如果需要保留记录不变,请在调用WriteRecords(...)之后再次添加标头。)

private void Savetocsv_Click(object sender, EventArgs e)
{
    var myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    using (var writer = new StreamWriter(myDocument + "/my-data.csv", append: true))
    {
        using (var csv = new CsvWriter(writer))
        {
            records.RemoveAt(0);  // Removes the header row.
            csv.WriteRecords(records);
        }                
    }
}

答案 4 :(得分:0)

我不知道您使用的是哪个CsvWriter,但是一个here具有HasHeaderRecord属性,您可以使用该属性忽略或包含标头。

private void Savetocsv_Click(object sender, EventArgs e)
{
    var myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    using (var writer = new StreamWriter(myDocument + "/my-data.csv", append: true))
    {
        using (var csv = new CsvWriter(writer))
        {
            csv.Configuration.HasHeaderRecord = true;
            csv.WriteRecords(records);
        }               
    }
}