9

Export data to Excel (xls) in Ruby on Rails

Posted by Dhaval Parikh on Apr 2, 2009

The code Below will help to export the data in 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>

Please note :- This method might might be deprecated. You may visit http://spreadsheet.rubyforge.org/ for more info. Thanks

Tags: , , ,

Copyright © 2012 Dhaval Parikh All rights reserved. Theme by Laptop Geek.