Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

Appending to an Existing CSV File with csvhelper

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

    // Write to a file.
    using (var writer = new StreamWriter("path	ofile.csv"))
    using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
    {
        csv.WriteRecords(records);
    }

    records = new List<Foo>
    {
        new Foo { Id = 2, Name = "two" },
    };

    // Append to the file.
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        // Don't write the header again.
        HasHeaderRecord = false,
    };
    using (var stream = File.Open("path	ofile.csv", FileMode.Append))
    using (var writer = new StreamWriter(stream))
    using (var csv = new CsvWriter(writer, config))
    {
        csv.WriteRecords(records);
    }
}

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}
Source by joshclose.github.io #
 
PREVIOUS NEXT
Tagged: #Appending #Existing #CSV #File #csvhelper
ADD COMMENT
Topic
Name
1+3 =