0

I am trying to export to a .csv file in UTF-8 encoding with semicolon as delimiter character:

context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent(); 
context.Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}", GetFileName(...)));
context.Response.ContentType = "Application/csv";
var encoding = Encoding.UTF8;
context.Response.ContentEncoding = encoding;
context.Response.BinaryWrite(encoding.GetPreamble());
context.Response.Output.Write("sep=;");  // tell Excel to interpret semicolon as separator
//csv file content creation here (using context.Response.Output.WriteLine)
context.Response.Flush();
context.Response.End();

The problem is that lines:

context.Response.BinaryWrite(encoding.GetPreamble());
context.Response.Output.Write("sep=;");

don't work properly together:

enter image description here

Using only first line I can resolve issue with encoding, when Excel displays Unicode characters like ÐБ ОРÐ. But without second line Excel displays all data in the first column.

Could you help me to understand how to force Excel to use UTF-8 encoding and ; as a separator?

pnuts
  • 58,317
  • 11
  • 87
  • 139
akekir
  • 523
  • 4
  • 9
  • 2
    Did you see [this answer](http://stackoverflow.com/a/23513342/231316) that says that the only way to force the encoding and delimiter reliably is to force UTF16 LE? – Chris Haas Sep 22 '15 at 14:49
  • @user1666620 obviously that will only work if the characters in play are from the Western latin character set. – Alastair McCormack Sep 22 '15 at 18:37
  • The duplicate question describes precisely the difficulty in using UTF8 for character-delimited file while also trying to override the default delimiter character. If you still believe that question does not address your own, please provide [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) that reliably reproduces the problem, along with a precise description of what that code does and how that's different from what you want. – Peter Duniho Sep 22 '15 at 23:18

0 Answers0