I tried to write to CSV file using CsvHelper in C#.
This is the link to the library http://joshclose.github.io/CsvHelper/
I used the code in web site.
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 a part of data to csv file.
Last rows were missing.
Could you please help with this.
Many contributors have helped make CsvHelper the great library it is today. Completely free for commercial use.
First you'll need to create a new Visual Studio C# console application, there are steps to follow to do this. The example code will create a csv file called MyTest. csv in the location you specify. The contents of the file should be 3 named columns with text in the first 3 rows.
To save an Excel file as a comma-delimited file: From the menu bar, File → Save As. Next to “Format:”, click the drop-down menu and select “Comma Separated Values (CSV)” Click “Save”
You need to flush the stream. The Using statement will flush when out of scope.
using (TextWriter writer = new StreamWriter(@"C:\test.csv", false, System.Text.Encoding.UTF8)) { var csv = new CsvWriter(writer); csv.WriteRecords(values); // where values implements IEnumerable }
when, I added this code after the loop code is working well
var csv = new CsvWriter(writer); csv.Configuration.Encoding = Encoding.UTF8; foreach (var value in valuess) { csv.WriteRecord(value); } writer.Close();
The problem occurred because I did not close the Connection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With