Character encoding issue exporting rails data to CSV

前端 未结 4 1052
后悔当初
后悔当初 2020-12-03 08:12

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
<         


        
相关标签:
4条回答
  • 2020-12-03 08:35

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

    0 讨论(0)
  • 2020-12-03 08:38

    This is my approach using I18n.transliterate:

    def self.to_csv(options = {})
      csv = CSV.generate(options) do |csv|
        csv << your_attributes
      end
      I18n.transliterate(csv)
    end
    

    I18n.transliterate just removes strange characters, and tries to make them readable (for example it will replace á with a, ö with o, etc). Just give it a try.

    0 讨论(0)
  • 2020-12-03 08:42

    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

    0 讨论(0)
  • 2020-12-03 08:53

    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 using BOM with UTF8 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/226255

    def self.to_csv(options = {})
      (CSV.generate(options) do |csv|
        csv << self.headers
    
        all.each do |e|
          csv << e.values
        end
       end).encode('WINDOWS-1252', :undef => :replace, :replace => '')
    end
    
    0 讨论(0)
提交回复
热议问题