-->

Classic ASP - how to save data to CSV file with UT

2020-07-27 04:03发布

问题:

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?