C#写入文件时使用UTF8
问题描述:
我有这个简单的代码:C#写入文件时使用UTF8
using (var sw = new StreamWriter(path + kat + "/" + namec, true, Encoding.UTF8))
{
sw.WriteLine(namex + "," + address + "," + web + "," + email + "," + phone + "," + linksx + "," + transport);
}
我使用Selenium网络驱动程序从丹麦网站,例如链接下载数据:http://www.visitdenmark.dk/da/danmark/danhostel-roenne-gdk614558
然后我把它保存在.csv文件,但我仍然有这样的角色,而不是这个角色。正如你所看到的,我设置了Encoding.UTF8
当我设置bool append为false时,它也变得更有趣了,那么一切都是正常的,但它不会帮助我,因为我需要用新数据追加该文件。我怎样才能解决这个问题 ?
答
对于丹麦语,您应该使用windows-1252(或Encoding.GetEncoding(1252)
)而不是UTF-8。
编辑:
它缺少BOM问题造成的Excel无法正确读取文件。
// notice we are creating a new file here
using (var writer = new StreamWriter(path, false, Encoding.UTF8))
{
// Add a BOM in the beginning of the file
var preamble = Encoding.UTF8.GetPreamble();
writer.BaseStream.Write(preamble, 0, preamble.Length);
// write data...
writer.WriteLine(string.Join(",", "Danhostel Rønne", "Arsenalvej 12 3700 Rønne"));
}
// when you write the same file again, you don't need to append the BOM again
using (var writer = new StreamWriter(path, true, Encoding.UTF8))
{
// write more data...
writer.WriteLine(string.Join(",", "Danhostel Rønne", "Arsenalvej 12 3700 Rønne"));
}
还是一样http://prntscr.com/f16v7b当我做'MessageBox.Show(namex);'以.csv文件中插入之前显示我正常的字符,以便插入之前一切正常。任何线索? – mdieod
@mdieod你能测试这段代码是否能产生正确的结果吗? https://pastebin.com/L947k1Nw – Xiaoy312
我只是运行你的代码,并得到相同的结果http://prntscr.com/f176iv – mdieod