Haml - Export CSV

From Sandoz

Jump to: navigation, search

Code using Fastercsv:

  def export_to_csv
   csv_string = FasterCSV.generate(:col_sep => ",") do |csv|
     members = ["Versions / Project Members"]
     members_selected.each {|member| members << Stat.member_name(member)}
     Stat.project_members(project).each {|user| members << user.name}
     csv << ["some text", "text 2", "text 3"]
   end
   return csv_string
  end

and this is how I am sending the data:

 send_data(export_to_csv,:type => 'text/csv; charset=iso-8859-1; header=present',
                    :disposition => "attachment", :filename => "filename.csv")


This is what I am doing using CSV::Writer:

  def export_to_csv
    report = StringIO.new
     CSV::Writer.generate(report, ',') do |csv|
       csv << ['c1', 'c2']
     end
 end

and call it as:

send_data(export_to_csv,:type => 'text/csv; charset=iso-8859-1; header=present',
                    :disposition => "attachment", :filename => "filename.csv")

http://stackoverflow.com/questions/1798071/export-to-csv-using-fastercsv-and-csvwriter-ruby-on-rails http://oldwiki.rubyonrails.org/rails/pages/HowtoExportDataAsCSV http://rubyrailsandwindows.blogspot.com/2009/06/rails-view-export-to-csv.html

Personal tools