简体   繁体   English

CSV在滑轨中显示奇怪的撇号

[英]CSV shows weird apostrophe in rails

I have included CSV import feature in my rails app. 我已在Rails应用程序中包含CSV导入功能。 But the issue I am facing is if there is any industry with includes apostrophe in title, it shows up weird in CSV. 但是我面临的问题是,如果任何行业的标题中都包含撇号,那么它在CSV中就会显得很奇怪。 How can I add meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" so that CSV includes apostrophe. 如何添加元http-equiv =“ Content-Type” content =“ text / html; charset = ISO-8859-1”,以便CSV包含撇号。

CSV_HEADER = %w[title role team]



def self.to_csv
      CSV.generate do |csv|
      csv << CSV_HEADER
      all.each do |industry|
      csv << [
        industry.title,        
        industry.role, 
        industry.team
       ]
      end
    end 
 end


<%= link_to "Download CSV", admin_path(@industry, :format => :csv, :filterrific => @filterrific.to_hash), class: "btn btn-primary btn-2x" %>

controller action 控制器动作

def application
    require 'csv'

    add_breadcrumb "Admin", :main_admin_path
    add_breadcrumb "Industrial Applications"
    @filterrific = initialize_filterrific(
      Industry,
      params[:filterrific],
      select_options: {
    with_status: Industry.options_for_status
        }
    )   or return   
    @industries = @filterrific.find
    @industries = @industries.paginate(:page => params[:page], :per_page => 10) unless request.format == 'csv' #does pagination if not csv format

    respond_to do |format|
      format.html
      format.js
      format.csv { send_data @industries.to_csv, :type => 'text/csv; charset=iso-8859-1; header=present' }
    end
    rescue ActiveRecord::RecordNotFound => e
        puts "Had to reset filterrific params: #{ e.message }"
        redirect_to(reset_filterrific_url(format: :html)) and return            
end

BOM(字节顺序标记)的概念用于解决此问题。

format.csv { send_data "\uFEFF" + @industries.to_csv, type: :csv }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM