How we can write delimiter like sep=, using CsvHelper library?

user3473830 picture user3473830 · May 25, 2015 · Viewed 9.9k times · Source

we're using CsvHelper library to export some information from our application, our clients normally use Excel to see the results

enter image description here (sample of correctly opened data)

everything was working well until I tested my generated files in another machine with Format set on German(Austria) which I found out that excel would not parse it correctly anymore which is understandable because , has a different meaning in this format. enter image description here

adding sep=, in the first line seems to fix the issue, but I couldn't find in CsvHelper documents that How we can achieve this. so the question is

How we can write delimiter like sep=, or anything with similar effect using CsvHelper library?

Answer

xanatos picture xanatos · May 25, 2015

Inside the CsvWriter class there is an aptly named WriteExcelSeparator() that should do it.

Depending on how you use the library, you can even:

csv.Configuration.Delimiter = ",";
csv.Configuration.HasExcelSeparator = true;

If you use the WriteRecords, use the second way, while if you use WriteHeader/WriteRecord use the first one.

csv.WriteExcelSeparator();
csv.WriteHeader<Simple>();
csv.WriteRecord( record );