35

Trying to write the contents of people to a CSVfile and then export it, however I am getting a build error and its failing. the error is:

cannot convert from 'System.IO.StreamWriter' to 'CsvHelper.ISerializer'

Not sure why this is happening unless as I am certain I have done it this way loads of times.

private void ExportAsCSV()
{
    using (var memoryStream = new MemoryStream())
    {
        using (var writer = new StreamWriter(memoryStream))
        {
            using (var csv = new CsvHelper.CsvWriter(writer))
            {
                csv.WriteRecords(people);
            }

            var arr = memoryStream.ToArray();
            js.SaveAs("people.csv",arr);
        }
    }
}
peterh
  • 11,875
  • 18
  • 85
  • 108
dros
  • 1,217
  • 2
  • 15
  • 31
  • 1
    This suggests `CsvHelper.CsvWriter(TextWriter)` is not in scope. Double check that you're getting the right version of the package, and that `StreamWriter` is the usual class (`System.IO.StreamWriter`). Use "Go to Definition" on `CsvWriter` to double-check. – Jeroen Mostert Jan 17 '20 at 12:57
  • @JeroenMostert did you mean to check csvWriter? i did and its belongs to the CSVhelper class which uses using CsvHelper.Configuration; using CsvHelper.TypeConversion; using System; using System.Collections; using System.Collections.Generic; using System.Dynamic; using System.Globalization; using System.IO; using System.Threading.Tasks; – dros Jan 17 '20 at 13:19
  • Yes, but what the compiler is telling you is that it is invoking the `CsvWriter` constructor that takes an `ISerializer`, and failing since there's no conversion. It should have picked the `CsvWriter` constructor that takes a `TextWriter`, since `StreamWriter` inherits from that, so either that constructor is missing (for whatever reason) or the compiler's overload resolution is broken (a little less probable, but weirder things have happened). – Jeroen Mostert Jan 17 '20 at 13:25

1 Answers1

81

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 = new StreamWriter(memoryStream))
        {
            using (var csv = new CsvHelper.CsvWriter(writer, System.Globalization.CultureInfo.CurrentCulture)
            {
                csv.WriteRecords(people);
            }

            var arr = memoryStream.ToArray();
            js.SaveAs("people.csv",arr);
        }
    }
}

Note: CultureInfo.CurrentCulture was the default in previous versions.

Consider

  • CultureInfo.InvariantCulture - If you control both the writing and the reading of the file. That way it will work no matter what culture the user has on his computer.
  • CultureInfo.CreateSpecificCulture("en-US") - If you need it to work for a particular culture, independent of the user's culture.
InteXX
  • 6,135
  • 6
  • 43
  • 80
David Specht
  • 7,784
  • 1
  • 22
  • 30