Classic ASP - how to save data to CSV file with UTF-8

对着背影说爱祢 提交于 2020-01-01 18:20:52

问题


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...


回答1:


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

  • Microsoft Excel mangles Diacritics in .csv files?


来源:https://stackoverflow.com/questions/25704440/classic-asp-how-to-save-data-to-csv-file-with-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!