c#, Excel + csv: how to get the correct encoding?

前端 未结 7 1209
有刺的猬
有刺的猬 2020-12-20 12:12

I\'ve been trying this for quite a while now, but can\'t figure it out. I\'m trying to export data to Excel via a *.csv file. It works great so far, but I have some encodi

7条回答
  •  一整个雨季
    2020-12-20 12:54

    "ANSI as UTF8"(WTF?)

    NotePad++ is probably correct. The encoding is UTF8 (i.e., correct Unicode header), but only contains ANSI data (i.e., é is not encoded in correct UTF8 way, which would mean two bytes).

    Or: it is the other way around. It is ANSI (no file header BOM), but the encoding of the individual characters is, or looks like, UTF8. This would explain the ü and other characters expanding in more than one other character. You can fix this by forcing the file to be read as Unicode.

    If it's possible to post (part of) your CSV, we may be able to help fixing it at the source.

    Edit

    Now that we've seen your code: can you remove the StreamWriter and replace it with a TextWriter? Also, remove the hand-encoding of the BOM, it is not necessary. When you create a TextWriter, you can specify the encoding (don't use ASCII, try UTF8).

提交回复
热议问题