I'm exporting data to a CSV file in rails and in some of my fields, I'm getting character encoding issues like this when I open in Excel:
didn’t
I borrowed this code from an example and I'm assuming the encoding is off. Any idea what it should be?
send_data csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{filename}.csv"
This worked for me, with Chinese characters!excel csv fromat (BOM + UTF8)
source(Chinese): http://blog.inheart.tw/2013/09/rubyraisl-csv-excel.html
The answers above did not work for me on Mac Excel: Using
iso-8859-1
would require I replace/remove weird characters, which is not a good enough solution for me, and usingBOM
withUTF8
worked under Windows but not under Mac Excel.What worked for me is the
WINDOWS-1252
encoding as suggested by https://stackoverflow.com/a/20194266/226255When Excel opens the CSV file it just assumes an "iso-8859-1" character encoding. I guess it doesn't even know about the encoding information you send along within your HTTP reply. That's why setting this to UTF-8 doesn't work.
So in order to export your CSV file for Excel in Rails you could do this:
This re-encodes your UTF-8 data string (that's the Rails default) to ISO-8859 and sends it. Along goes the information that this reply is actually ISO-8859-1 encoded (which won't make a difference for Excel but is technically correct if you should open it in a browser etc.).
This is my approach using
I18n.transliterate
:I18n.transliterate
just removes strange characters, and tries to make them readable (for example it will replaceá
witha
,ö
witho
, etc). Just give it a try.