加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 00:06:36 所属栏目:百科 来源:网络整理
导读:编写了将数据导出到csv文件的方法. ??如果单元格的值在csv中有DEC20,则为20-Dec,这是不正确的. 我的代码是这样的: for (int i = 0; i myData.Count(); i++){ sb.AppendLine(string.Join(delimiter,Common.FormatExportString(myData[i].Code),Common.Format
编写了将数据导出到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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读