Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

csvhelper

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?

like image 234
user3473830 Avatar asked May 25 '15 11:05

user3473830


2 Answers

They first changed it to being a parameter of CsvConfiguration and as of 8th of March 2021 it is

var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ";", Encoding = Encoding.UTF8 };
using var csv = new CsvReader(reader, config);

Likely they will change it again in the future ;)

like image 163
CodingYourLife Avatar answered Sep 18 '22 12:09

CodingYourLife


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 );
like image 22
xanatos Avatar answered Sep 21 '22 12:09

xanatos