musa
musa

Reputation: 339

How to exclude header when writing data to CSV

I am writing my data from a public class to a CSV file. As I want to append my data, I want to exclude the importing of header and only import the data from the class. My code below imports both headers and data. Hope to get help. Thanks.

Record.cs - my class

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

    public DateTime DateOfBirth
    {
        get; set;
    }
}

Form1.cs - my form

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

Upvotes: 12

Views: 14694

Answers (5)

Cakemeister
Cakemeister

Reputation: 291

The name of the configuration class has changed.

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

Upvotes: 11

Eric McLachlan
Eric McLachlan

Reputation: 3530

I don't know which CsvWriter you are using, but the one here has a HasHeaderRecord property that you can use to ignore or include headers.

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

Upvotes: 1

xdtTransform
xdtTransform

Reputation: 2057

Using the Configuration , you can use the property HasHeaderRecord:

HasHeaderRecord : Gets or sets a value indicating if the CSV file has a header record.
Default is 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);
}

Result file : "file.csv"

1;one
1;one

Or simply loop on records an write them:

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

Upvotes: 12

Risto M
Risto M

Reputation: 2999

Change your writing method as following and then CsvHelper.WriterConfiguration do the trick (note 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);
        }                
    }
}

Upvotes: 1

Eric McLachlan
Eric McLachlan

Reputation: 3530

Remove the first row from records before calling:

csv.WriteRecords(records);

(If you need to leave records unchanged, add the headers back again after calling 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);
        }                
    }
}

Upvotes: 0

Related Questions