从 C# 创建 CSV 文件:Excel 中的额外字符

发布于 2024-10-16 12:55:59 字数 1582 浏览 1 评论 0原文

我从 C# 应用程序创建了一个 CSV 文件,但字符  显示在 Excel 和 OpenOfficeCalc 的第一个单元格中,但不显示在记事本和 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 is

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

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

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 中:

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 个字符仅在文件开头出现一次,然后一切都应如此。

我的问题是:

 从哪里来以及如何删除它?

我尝试在 StringBuilder 中编写输出并进行调试以查看其内容,但它没有这些字符。

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++.

Here is my code:

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 is

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

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

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

My question is:

Where does  come from and how to remove it?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墟烟 2024-10-23 12:55:59

这是UTF8编码的BOM

看这个问题: Write text files without字节顺序标记(BOM)?

This is the BOM for UTF8 encoding

Look at this question: Write text files without Byte Order Mark (BOM)?

感情废物 2024-10-23 12:55:59

您可能需要根据您的输入指定编码类型。
StreamWriter.Encoding 属性

http://msdn.microsoft.com /en-us/library/system.io.streamwriter.encoding.aspx

You may want to specify the encoding type based on your input.
StreamWriter.Encoding Property

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.encoding.aspx

攀登最高峰 2024-10-23 12:55:59

我认为那是BOM(unicode header)。在 StreamWriter 的构造函数中指定 ANSI 编码。很简单:只需将其设置为 Encoding.Default 即可。

更新:
如果您必须使用 UTF-8 并且需要去掉开头的这 3 个字节,请使用:new UTF8Encoding(false)。这样写入器将保留 UTF8,但不会写入 BOM。

I think that is BOM (unicode header). Specify ANSI encoding in constructor of StreamWriter. It is simple: Just set it to Encoding.Default.

update:
If you must use UTF-8 and need to get rid of those 3 bytes at the beginning, use this: new UTF8Encoding(false). This way the writer will stay in UTF8, but won't write BOM.

雪化雨蝶 2024-10-23 12:55:59

创建 StreamWriter 时尝试指定 Encoding.ASCII 而不是使用 Encoding.UTF8

Try to specify Encoding.ASCII instead of using Encoding.UTF8 when you create the StreamWriter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文