为什么我不能从“System.IO.StreamWriter”转换为“CsvHelper.ISerializer”?

dro*_*ros 31 c# csvhelper .net-core

试图将人们的内容写入 CSV 文件,然后将其导出,但是我遇到了构建错误并且失败了。错误是:

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

不知道为什么会发生这种情况,除非我确定我已经这样做了很多次。

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*cht 76

13.0.0 版发生了重大变化。本地化存在很多问题,因此@JoshClose 要求用户指定CultureInfo他们想要使用的。您现在需要CultureInfo在创建CsvReaderCsvWriter. 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意: CultureInfo.CurrentCulture是以前版本中的默认值。

考虑

  • CultureInfo.InvariantCulture- 如果您控制文件的写入和读取。这样,无论用户在他的计算机上有什么文化,它都可以工作。
  • CultureInfo.CreateSpecificCulture("en-US")- 如果您需要它为特定文化工作,独立于用户的文化。

  • 你节省了我很多时间,谢谢,David *适用于 v15.0.0 (2认同)