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

前端 未结 7 1185
有刺的猬
有刺的猬 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:37

    This solution is written up as a fix for a Java application however you should be able to do something similar in C#. You may also want to look at the documentation on the StreamWriter class, in the remarks it refers to the Byte Order Mark (BOM).

    0 讨论(0)
  • 2020-12-20 12:42

    try the following:

    using (var sw = File.Create(Path.Combine(txtPath.Text, "UTF8.csv")))
    {
      var preamble = Encoding.UTF8.GetPreamble();
      sw.Write(preamble, 0, preamble.Length);
      var data = Encoding.UTF8.GetBytes("懘荧,\"Hello\",text");
      sw.Write(data, 0, data.Length);
    }
    

    It writes the proper UTF8 preamble to the file before writing the UTF8 encoded CSV.

    0 讨论(0)
  • 2020-12-20 12:46

    Trevor Germain's helped me to save in the correct encoded format

    using (var sw = File.Create(Path.Combine(txtPath.Text, "UTF8.csv")))
    {
        var preamble = Encoding.UTF8.GetPreamble();  
        sw.Write(preamble, 0, preamble.Length);  
        var data = Encoding.UTF8.GetBytes("懘荧,\"Hello\",text");  
        sw.Write(data, 0, data.Length);
    }
    
    0 讨论(0)
  • 2020-12-20 12:53

    I'd suggest you open up the text file in a hex editor, and see what it really is. The BOM for UTF-16 is 0xFEFF, which the writing code is apparently writing to the stream - but the rest of the writing doesn't specify an encoding to use - it would use the default encoding of the StreamWriter, which is UTF-8. There appears to be a mix up of encodings.

    When you pop open the file in hex view, if you see lots of 0x00 between the characters, you're working with UTF-16, which is Encoding.Unicode in C#. If there are no 0x00 between chars, the encoding is probably UTF-8.

    If the latter case, just fix up the BOM to be EF BB BF rather than FE FF, and read normally with UTF-8 encoding.

    0 讨论(0)
  • 2020-12-20 12:54

    This worked perfect for me:

    private const int WIN_1252_CP = 1252; // Windows ANSI codepage 1252
    
        this._writer = new StreamWriter(fileName, false, Encoding.GetEncoding(WIN_1252_CP));
    

    CSV encoding issues (Microsoft Excel)

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题