CsvHelper Ignore case for header names

If you are using the http://joshclose.github.io/CsvHelper/ you can provide some configuration when constructing the CsvReader or configuring it after construction. using (var stringReader = new StringReader(yourString)) using (var csvReader = new CsvReader(stringReader)) { // Ignore header case. csvReader.Configuration.PrepareHeaderForMatch = (string header, int index) => header.ToLower(); return csvReader.GetRecords<Import>().ToList(); } There is more documentation in the PrepareHeaderForMatch … Read more

Parse CSV where headers contain spaces with CsvHelper

Based on CsvHelper Documentation, there are several ways that we can achieve our desired results. 1. Ignore White Space from Headers (which I believe should solve your problem easily) In CsvHelper 3 or later, use PrepareHeaderForMatch (documented at http://joshclose.github.io/CsvHelper/configuration#headers) to remove whitespace from headers: csv.Configuration.PrepareHeaderForMatch = header => Regex.Replace(header, @”\s”, string.Empty) In CsvHelper 2, set … Read more

Why can’t I convert from ‘System.IO.StreamWriter’ to ‘CsvHelper.ISerializer’?

There was a breaking change with version 13.0.0. There have been many issues with localization, so @JoshClose is requiring users to specify the CultureInfo they want to use. You now need to include CultureInfo when creating CsvReader and CsvWriter. https://github.com/JoshClose/CsvHelper/issues/1441 private void ExportAsCSV() { using (var memoryStream = new MemoryStream()) { using (var writer = … Read more

In CsvHelper how to catch a conversion error and know what field and what row it happened in?

Currently, there is no way to ignore errors at the field/property level. Your current options are these: Look at the exception data. catch( Exception ex ) { // This contains useful information about the error. ex.Data[“CsvHelper”]; } Ignore reading exceptions. This is on a row level, though, not field. It will allow the whole file … Read more

Using CSVHelper to output stream to browser

Try below code: public FileStreamResult ExportPayments() { var result = WriteCsvToMemory(_commonService.GetPayments()()); var memoryStream = new MemoryStream(result); return new FileStreamResult(memoryStream, “text/csv”) { FileDownloadName = “export.csv” }; } public byte[] WriteCsvToMemory(IEnumerable<Payment_dto> records) { using (var memoryStream = new MemoryStream()) using (var streamWriter = new StreamWriter(memoryStream)) using (var csvWriter = new CsvWriter(streamWriter)) { csvWriter.WriteRecords(records); streamWriter.Flush(); return memoryStream.ToArray(); } … Read more

CsvHelper changing how dates and times are output

With newer version (12.1.2) of CsvHelper, it can be achieved by using TypeConverterOptionsCache var options = new TypeConverterOptions { Formats = new[] { “MM/dd/yyyy” } }; csvWriter.Context.TypeConverterOptionsCache.AddOptions<DateTime>(options); Output date 08/24/1991 Version 20 moved TypeConverterOptionsCache from Configuration to Context. So the above becomes var options = new TypeConverterOptions { Formats = new[] { “MM/dd/yyyy” } }; … Read more

CsvHelper not writing anything to memory stream

You already have a using block which is great. That will flush your writer for you. You can just change your code slightly for it to work. using (var memoryStream = new MemoryStream()) { using (var streamWriter = new StreamWriter(memoryStream)) using (var csvWriter = new CsvWriter(streamWriter)) { csvWriter.WriteRecords<T>(records); } // StreamWriter gets flushed here. return … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)