尝试将人们的内容写入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);
        }
    }
}

最佳答案

版本13.0.0发生了重大变化。本地化存在很多问题,因此@JoshClose要求用户指定他们要使用的CultureInfo。现在,在创建CultureInfoCsvReader时需要包括CsvWriterhttps://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是先前版本中的默认设置。
考虑
  • CultureInfo.InvariantCulture-如果您控制文件的写入和读取。这样,无论用户在计算机上使用哪种文化,它都将起作用。
  • CultureInfo.CreateSpecificCulture("en-US")-如果您需要它来用于particular culture,则与用户的文化无关。
  • 关于c# - 为什么我不能从 'System.IO.StreamWriter'转换为 'CsvHelper.ISerializer'?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59787783/

    10-12 06:05