Character encoding issue exporting rails data to CSV

This worked for me, with Chinese characters!excel csv fromat (BOM + UTF8)

def export_csv_excel
  ....

  # Add BOM to make excel using utf8 to open csv file
  head = 'EF BB BF'.split(' ').map{|a|a.hex.chr}.join()

  csv_str = CSV.generate(csv = head) do |csv|
    csv << [ , , , ...]
    @invoices.each do |invoice|
      csv << [ , , , ...]
    end
  end

  send_data csv_str, filename: "Invoices-#{Time.now.strftime("%y%m%d%H%M%S")}.csv", type: "text/csv"
end

source(Chinese): http://blog.inheart.tw/2013/09/rubyraisl-csv-excel.html


When 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:

send_data Iconv.conv('iso-8859-1//IGNORE', 'utf-8', csv_data),
  :type => 'text/csv; charset=iso-8859-1; header=present',
  :disposition => "attachment; filename=#{filename}.csv"

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