2

I have the following code:

output = ...
(some comma-separated data)

Response.Clear
'Response.ChartSet = "UTF-8"
'Response.CodePage = 65001
Response.ContentType = "text/csv"
Response.AddHeader "Content-Disposition", "filename=myfile.csv;"
Response.Write(output)
Response.End

Now, everything is working just fine with having the data itself and generating a virtual CSV file for direct download, but if I have a non-ascii data in one or more of the fields (columns), I don't get it in UTF-8 in the generated UTF-8.

How can I set the content of the generated file to be in UTF-8??

Each of the commented lines in this code doesn't seem to affect the output...

user692942
  • 16,398
  • 7
  • 76
  • 175
TheCuBeMan
  • 1,954
  • 4
  • 23
  • 35
  • 1
    No not another one I'm not answering it, someone else can. Seriously just search it's been answered more times then I care to count! Just check out the first three results in [Google](https://www.google.com/search?sourceid=chrome-psyapi2&ion=1&espv=&ie=UTF-8&q=classic%20asp%20utf-8). Also [this answer](http://stackoverflow.com/a/17680939/692942) should help. My guess your page isn't saved as `UTF-8` so IIS isn't processing it as such. Also you have a typo it's `Response.Charset = "UTF-8"` not `Response.Chartset`. – user692942 Sep 08 '14 at 10:19
  • 1
    First of all, perhaps I forgot to mention it in the original question, but the first thing I did when I encountered this issue is to make sure I'm saving the file itself as UTF-8. Btw, the typo here is just in writing the piece of code in the question, not in my code... I will try what you suggest here in the answer... Thanks. – TheCuBeMan Sep 08 '14 at 14:24

1 Answers1

8

Further to my comments if you are opening your CSV in Excel and wondering why the UTF-8 encoding isn't working, don't worry this is a side effect of how Excel identifies UTF-8 CSV data.

Try adding this before writing your output

'BOM to dup Excel into encoding the CSV correctly instead of using ASCII!!!
Call Response.BinaryWrite(ChrB(239) & ChrB(187) & ChrB(191))

Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175