Last week, I created a web page that allowed some users to download a list of name with their email addresses as a CSV file. However, some characters were not exported correctly.
Here is a sample of the code I used :
StringBuilder sb = new StringBuilder();
IList<Subscriber> subscribers = Subscriber.GetAll();
foreach (Subscriber subscriber in subscribers)
{
sb.AppendLine(string.Format("{0},{1}", subscriber.Email, subscriber.Name.Replace(",", "")));
}
Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"subscribers.csv\"");
Response.Write(sb.ToString());
Response.End();
And some name looked like this when opened in Excel : sébastien lachance. Not really the expected results.
Why is this occurring?
The default encoding for the response stream is UTF-8 (8-bit UCS/Unicode Transformation Format) and Excel will not detect it due to the lack of BOM (Byte Order Mark). Instead, it will try to open the file using the ASCII encoding. It appear to be a known bug and the fix for it is easy.
Response.ContentEncoding = Encoding.Unicode;
or
Response.Write("\uFEFF");
Just before writing to the stream.