(simple) CSV download with special characters drives me nuts

this one turns me on. And last week I took apart the nut, but, unfortunately, not ...

so I want to create a CSV download for other users so that they open in MS-Word (they want the CSV format)

so I get this code in MVC2 controller:

Response.AddHeader("Content-Disposition", "attachment; filename=PersonalMessages.csv"); Response.ContentType = "application/csv"; string s = new DownloadService().GetAddresses(); Response.Write(s); Response.End(); return null; 

the string 's' contains (among other information) these characters: é å æ É à

When I open it in notepad: OK

when I open it in Notepad ++: Ok

when I open it in Excel: not ok, it shows it: Ã © Ã ¥ Ã | Ã ‰ Ã

(when I open it in Notepadd ++, it says that it is in UTF8 encoding without specification, no matter what I tried with UTF8 encodings with boolean in constructor, to get the specification, it did not work)

So, I asked a question last week, and the answer was that the GetAddresses () function would return a byte array.

So, I converted my text using this:

 // C# to convert a string to a byte array. public static byte[] StrToByteArray(string str) { System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding(); return encoding.GetBytes(str); } 

and wrote an array of bytes in the response, and everything was fine! Notepad, Notepad ++ and Excel! Great, live well.

But then I found out that we open the file in Word. Well, no problem, I thought.

But then they help: Word cannot open the file (directly), it asks for the first encoding , and this is a problem because they use it in an automatic process.

When they open the file in Notepad and save it as Unicode, everything goes well.

I also tried this in the action method:

 Response.ContentEncoding = Encoding.Unicode ; Response.HeaderEncoding = Encoding.UTF8; 

but it did not help.

Are there any clues?

+4
source share
3 answers

Try using ISO-8859-1 instead of UTF-8 :

 public ActionResult Index() { var encoding = Encoding.GetEncoding("iso-8859-1"); var data = encoding.GetBytes("éåæÉà;some other value"); return File(data, "application/csv", "PersonalMessages.csv"); } 

Also note the slight simplification and use of FileResult .

Another possibility that I would recommend to you is to explicitly add a preamble so that Excel can recognize UTF-8 encoding:

 public ActionResult Index() { var data = Encoding.UTF8.GetBytes("éåæÉà;some other value"); var result = Encoding.UTF8.GetPreamble().Concat(data).ToArray(); return File(result, "application/csv;charset=utf-8", "PersonalMessages.csv"); } 
+10
source

Use ISO-8859-1 instead of UTF-8

It works well.

I tried in an xlsx document.

+2
source

I tried many encodings, one that worked, Windows-1252 :

 Response.Clear(); Response.ContentType = "Application/x-msexcel"; Response.AddHeader("content-disposition", "attachment; filename=\"filename.csv\""); Response.ContentEncoding = System.Text.Encoding.GetEncoding("Windows-1252"); Response.Write(string.Join(Environment.NewLine, myDataLines)); Response.End(); 
+1
source

All Articles