Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Export to Excel (See related posts)

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


View (link to export)
<%= link_to "Export as Excel", export_person_url %>


View (Export)
#Render partial so your Layout wont be sent to the spreadsheet
<%= render :partial => 'report' %>


Partial
<table border="1">
  <tr>
    <th>Name</th>
  </tr>
  <% @person.each do |p| %>
    <tr>
      <td><%= p.name %></td>
    <% end %>
  </tr>
</table>


Comments on this post

VoteForPedro posts on Mar 17, 2009 at 19:04
In order to prevent your layout from being sent to the spreadsheet, how about just render :layout => false in your controller?

You need to create an account or log in to post comments to this site.