写入CSV文件时的C#格式列(单元格格式)

写入CSV文件时的C#格式列(单元格格式),第1张

概述编写了将数据导出到csv文件的方法.   如果单元格的值在csv中有DEC20,则为20-Dec,这是不正确的. 我的代码是这样的: for (int i = 0; i < myData.Count(); i++){ sb.AppendLine(string.Join(delimiter, Common.FormatExpor 编写了将数据导出到csv文件的方法.
  如果单元格的值在csv中有DEC20,则为20-Dec,这是不正确的.

我的代码是这样的:

for (int i = 0; i < myData.Count(); i++){    sb.Appendline(string.Join(delimiter,Common.FormatExportString(myData[i].Code),Common.FormatExportString(myData[i].name),Common.FormatExportString(myData[i].Description)));}//returns the file after writing the stream to the csv file.return file(new System.Text.UTF8EnCoding().GetBytes(sb.ToString()),"text/csv",filename);

请帮助我使用C#获取要在excel(csv)文件中显示的确切字符串格式.

例如:
myData [i] .name数据将是

DEC20,or 2-5

像这样,

但csv(excel)中的输出显示为

20-Dec and 5-May

而不是原来的.

解决方法 问题与csv导出没有关系,如果你用记事本打开csv文件就很好了. Excel将自动检测单元格类型作为日期并将其显示为日期.

要避免此行为,请在引号之间包装文本并在其前面加上a =,如下所示:

= "DEC20"

该文件应该成为

= "fIEld1",= "fIEld2",= "fIEld...",= "DEC20",...

你的方法应该成为:

for (int i = 0; i < myData.Count(); i++){    sb.Appendline(string.Join(delimiter," = \"",Common.FormatExportString(myData[i].Description)                              "\""));}//returns the file after writing the stream to the csv file.return file(new System.Text.UTF8EnCoding().GetBytes(sb.ToString()),filename);
总结

以上是内存溢出为你收集整理的写入CSV文件时的C#格式列(单元格格式)全部内容,希望文章能够帮你解决写入CSV文件时的C#格式列(单元格格式)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1222388.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存