we're using CsvHelper library to export some information from our application, our clients normally use Excel to see the results
(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.
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?
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 ;)
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 );
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