上回有討論過Excel開啟CSV時的中文編碼問題,今天發現關於.NET處理BOM的幾個特性,再補充三則筆記:

  • 雖然預設UTF8Encoding的encoderShouldEmitUTF8Identifier參數預設為true,但GetBytes()的結果不會包含BOM
  • File.WriteAllText與StreamWriter在沒有指定Encoding.UTF8時,會產出UTF-8編碼但沒有BOM的檔案
  • 以下的範例中,只有F2.csv、F4.csv可以正確被Excel開啟,原因請見上回文章
string s = "牛,牪,犇";
File.WriteAllText("B:\\F1.csv", s);
File.WriteAllText("B:\\F2.csv", s, Encoding.UTF8);
using (StreamWriter sw = 
    new StreamWriter("B:\\F3.csv", false))
{
    sw.WriteLine(s);
    sw.Close();
}
using (StreamWriter sw =
    new StreamWriter("B:\\F4.csv", false, 
        Encoding.UTF8))
{
    sw.WriteLine(s);
    sw.Close();
}

Comments

Be the first to post a comment

Post a comment


69 - 21 =