JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.65k stars 1.05k forks source link

Table header is duplicate each time two files are merged using WriteRecordsAsync() #2147

Open AndreiOvidiuCiobanu opened 1 year ago

AndreiOvidiuCiobanu commented 1 year ago

Describe the bug Using CsvWriter, with .NET 7.0 and 30.0.1 library version, table header is duplicate each time two files are merged using WriteRecordsAsync()

To Reproduce Provide a small working example that shows the issue.

Expected behavior Only one time when HasHeaderRecord is set up to insert table header.

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

Kuubs commented 1 year ago

Hi @AndreiOvidiuCiobanu, I got the same problem. Did you manage to find out the problem?

Kuubs commented 1 year ago

Reproduce:

`using CsvHelper.Configuration; using CsvHelper; using System.Collections; using System.Globalization; using System.Text;

var outputRecords = new List { new ExampleRecord { KolomA = "yes", KolomB = "no" }, new ExampleRecord { KolomA = "no", KolomB = "no" }, };

var myCsvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ";", ShouldQuote = (fieldArgs) => false };

using (var stream = File.Create("file.txt")) { var writer = new StreamWriter(stream, Encoding.GetEncoding(28591)); var csvWriter = new CsvWriter(writer, myCsvConfiguration); csvWriter.Context.RegisterClassMap(); csvWriter.WriteHeader(); //Creates dublicate headers in version 30. In version 29 you only get one header. csvWriter.NextRecord(); csvWriter.WriteRecords((IEnumerable)outputRecords); writer.Flush(); } public class ExampleRecord { public string KolomA { get; set; } public string KolomB { get; set; } }

public class ExampleRecordClassMap : ClassMap { public ExampleRecordClassMap() { Map(m => m.KolomA).Index(05).Name("KOLOM_A"); Map(m => m.KolomB).Index(06).Name("KOLOM_B"); } }`