Technical
Creating Xls Files

Controller


class GedStatusController < ApplicationController

  def show
    if params.has_key? :xls
       headers['Content-Type'] = "application/vnd.ms-excel" 
       headers['Content-Disposition'] = 'attachment; filename="ged_status_report.csv"'
       headers['Cache-Control'] = ''
       render :template => "ged_status/show_csv",:layout => false
    end
  end

end

View

in show.rhtml


Client ID,Client Name
<% for r_s in @ged_report_statuses -%>
<%= r_s.client_id -%>,"<%= r_s.client_name -%>"<% end %>

The top part (Client ID,Client Name) will be recognized by excel as a header, and the rest as your data.

Things to remember

  1. Make sure you dont have spaces between fields
  2. Don’t forget to put -%>
  3. If field is numeric don’t stringify it.