3

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

2
  • For what it's worth, I checked out a few options to do exactly this and wound up using the spreadsheet gem (rubygems.org/gems/spreadsheet). The documentation is a bit meh, but it is quite powerful and I was able to extend it to fit my needs. Commented Apr 17, 2011 at 21:02
  • please google next time first (e.g 'rails export to excel' gives You same pages as people put for You in the comment section ;) ) Commented Jun 12, 2012 at 14:37

1 Answer 1

9

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>
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for you help! now i use the spreadsheet plugin to solve the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.