Writing to a file using CsvHelper in C #

I tried writing to a CSV file using CsvHelper in C #.
This is a link to the http://joshclose.imtqy.com/CsvHelper/ library

I used the code on the website.

Here is my code:

var csv = new CsvWriter(writer);
csv.Configuration.Encoding = Encoding.UTF8;
foreach (var value in valuess)
{
    csv.WriteRecord(value);
}

It writes only part of the data to the csv file.
The last lines were missing. Could you help with this.

+4
source share
3 answers

when, I added this code after the loop code works well

using(var writer = new StreamWriter("file.csv") {
    using(var csvWriter = new CsvWriter(writer))
    {
        csvWriter.WriteRecords(diff);
    }
}

The problem arose because I did not close Connection

+4
source

You need to clear the stream. Using statement will be hidden if it goes out of scope.

using (TextWriter writer = new StreamWriter(@"C:\test.csv"))
{
    var csv = new CsvWriter(writer);
    csv.Configuration.Encoding = Encoding.UTF8;
    csv.WriteRecords(values); // where values implements IEnumerable
}
+13
source

, writer TextWriter, , :

writer.Flush()

, .

+6

Source: https://habr.com/ru/post/1537335/


All Articles