欢迎来到cool的博客
7

Music box

Click to Start

点击头像播放音乐
新博客链接

rails导出CSV XLS 中文乱码解决办法


  CSV.generate(options) do |csv|
    csv << column_names
    all.each do |product|
      csv << product.attributes.values_at(*column_names)
    end
  end.encode('gb2312', :invalid => :replace, :undef => :replace, :replace => "?")


send_data xml_data.encode('utf-8'),
          :type => 'text/xml; charset=utf-8; header=present',
          :filename => "data.xml"

重点在最后一行

导出方法 http://railscasts.com/episodes/362-exporting-csv-and-excel

导入 http://railscasts.com/episodes/396-importing-csv-and-excel

返回列表