且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-08 19:23:33

13.0.0 版发生了重大变化.本地化存在很多问题,因此@JoshClose 要求用户指定他们想要使用的 CultureInfo.您现在需要在创建 CsvReaderCsvWriter 时包含 CultureInfo.https://github.com/JoshClose/CsvHelper/issues/1441

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);
        }
    }
}

注意: CultureInfo.CurrentCulture 是以前版本的默认设置.

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

考虑

  • CultureInfo.InvariantCulture - 如果您控制文件的写入和读取.这样,无论用户在他的计算机上使用什么文化,它都能正常工作.
  • CultureInfo.CreateSpecificCulture("en-US") - 如果您需要它为 特定文化,独立于用户的文化.
  • 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.