在rails上的ruby中导出Excel

Sma*_*olf 3 ruby-on-rails export-to-excel

我想将页面信息导出到Excel,谁能告诉我该怎么做?谢谢!

bre*_*dan 9

这样的事情可能会有所帮助:

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

调节器

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
Run Code Online (Sandbox Code Playgroud)

视图

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>
Run Code Online (Sandbox Code Playgroud)