Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Excel in ruby on rails

I want to export the page Info to Excel ,who can tell me how can i do this? thank you!

like image 576
Small Wolf Avatar asked Dec 05 '25 21:12

Small Wolf


1 Answers

Something like this might help:

http://blog.dhavalparikh.co.in/2009/04/export-to-excel-in-ruby-on-rails/

Controller

class UserController < ApplicationController
  def export
    headers['Content-Type'] = "application/vnd.ms-excel"
    headers['Content-Disposition'] = 'attachment; filename="report.xls"'
    headers['Cache-Control'] = ''
    @users = User.find(:all)
  end

View

export.html.erb

<%= link_to "Export as Excel", export_person_url %>

_report.html.erb

<table border="1">
  <tr>
    <th>Name</th>
  </tr>
  <% @users.each do |u| %>
  <tr>
   <td><%= u.name %></td>
  <% end %>
 </tr>
</table>
like image 128
brendan Avatar answered Dec 07 '25 12:12

brendan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!