Skip to main content

Posts

Showing posts from June, 2014

Rails generate Csv or xls files

def get_all_users
    csv = ""
    csv1="Id,Email"
    csv2 = ""
    csv << "#{csv1}\n"
    csv << "#{csv2}"
    @user = User.find(:all)
    @user.each do |x|
       csv2 << "#{x.id}"
       csv2 << ",\"#{x.email}\""
       csv2 << "\n"
     end
   csv << csv2
   send_data csv, :type => 'text/xls; charset=utf-8; header=present', :disposition => "attachment;filename=xyz.xls"
(or)
send_data csv, :type => 'text/csv; charset=utf-8; header=present', :disposition => "attachment;filename=xyz.csv"
end

In Views

<p><%=link_to "hello",:controller => "user" ,:action => "get_all_users"%></p>