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

编程入门 行业动态 更新时间:2024-10-10 16:22:23
本文介绍了为什么我不能从"System.IO.StreamWriter"转换为"CsvHelper.ISerializer"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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:

无法从"System.IO.StreamWriter"转换为"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); } } }

推荐答案

版本13.0.0发生了重大变化.本地化存在很多问题,因此@JoshClose要求用户指定他们要使用的 CultureInfo .现在,在创建 CsvReader 和 CsvWriter 时,需要包括 CultureInfo . github/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. github/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.

更多推荐

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

本文发布于:2023-11-15 19:50:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   IO   quot   System   ISerializer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!