从C#创建CSV文件:Excel中的多余字符 [英] Create CSV file from c#: extra character in excel

查看:78
本文介绍了从C#创建CSV文件:Excel中的多余字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从C#应用程序创建CSV文件,但是字符在Excel和OpenOfficeCalc中的第一个单元格中显示,但不在Notepad和Notepad ++中显示.

I create a CSV file from a C# application but the characters  are displayed in Excel and OpenOfficeCalc in the first cell but not in Notepad and Notepad++.

这是我的代码:

StreamWriter streamWriter = new StreamWriter(new FileStream(filePath, FileMode.Create), Encoding.UTF8);
List<MyData> myData = GetMyData;

foreach(MyData md in myData)
{
    streamWriter.WriteLine(md.Date + "," + md.Data1 + "," + md.Data2 + "," + md.Data3 + "," + md.Data4);
}
streamWriter.Flush();
streamWriter.Close();

MyData

public struct MyData
{
    public float Data1;
    public float Data2;
    public float Data3;
    public float Data4;
    public DateTime Date;
}

这是记事本和记事本++中的结果:

Here is the result in Notepad and Notepad++:

01/12/2010 00:04:00,0.08,78787.4,9.1,5
01/12/2010 00:09:00,0.07,78787.42,9.1,5
01/12/2010 00:14:00,0.06,78787.44,9.1,5
01/12/2010 00:19:00,1.45,78787.58,9.1,5
01/12/2010 00:24:00,2.13,78788.15,9.1,5
01/12/2010 00:29:00,1.72,78788.53,9,5
01/12/2010 00:34:00,0.89,78788.73,9,5

在Excel和Calc中:

And in Excel and Calc:

01/12/2010 00:04:00   0.08    78787.4     9.1    5
01/12/10 00:09           0.07    78787.42    9.1    5
01/12/10 00:14           0.06    78787.44    9.1    5
01/12/10 00:19           1.45    78787.58    9.1    5
01/12/10 00:24           2.13    78788.15    9.1    5
01/12/10 00:29           1.72    78788.53    9      5
01/12/10 00:34           0.89    78788.73    9      5

这3个字符在文件开始处仅出现一次,然后一切都应按原样进行.

Those 3 characters appears only once at the start of the file and then everything is as it should be.

我的问题是:

来自何处以及如何将其删除?

Where does  come from and how to remove it?

我试图将我的输出写在StringBuilder中,并进行调试以查看其内容,但其中没有这些字符.

I have tried to write my output in a StringBuilder and to debug to see its content and it does not have those character.

推荐答案

这是用于UTF8编码的BOM

This is the BOM for UTF8 encoding

看看这个问题:写没有字节顺序标记的文本文件( BOM)?

这篇关于从C#创建CSV文件:Excel中的多余字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆