Export to Excel in Ruby on Rails

By , March 11, 2009 11:35 pm

Below code 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>

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Webnews
  • Digg
  • del.icio.us
  • Reddit
  • Bloglines
  • LinkedIn
  • YahooMyWeb
  • Facebook
  • Google Bookmarks
  • Mixx
  • MySpace
  • Technorati
  • TwitThis

14 Responses to “Export to Excel in Ruby on Rails”

  1. Venkataraman says:

    Export to excel facility implemented does not work in mozilla and chrome(The data is displayed in the web page itself). Whereas in IE the Run | Save | Cancel box appears and displays correctly in Excel sheet.

    Any replies

    • Reva says:

      hi
      when i m using this code, it shows an error of
      “undefined local variable or method `export_person_url’ for ” can u please suggest a solution.

  2. Brijesh Shah says:

    This code works well in mozilla and chrome. I tested it in both.

  3. Lori M Olson says:

    Note this code will not work for versions of Excel 2007 and newer. Excel now checks to see if the content type matches the file extension, and will throw up a scary security exception to your users.

  4. wohaizai007 says:

    how to add a sheet?

  5. Jnanesh says:

    I am new to rails. Is this code generic or required some tweaking based on the applications. Because directly cut-paste is not working in case of my application.

    Thanks,
    Jnanesh

  6. Bilgo says:

    Hi,

    I Export some data from mysql to excel.Some excel document is export correctly. However some of excel document has character problem.Datas type is Turkish.

    Character problem Ex:

    “Yep Yeni Rakı Bunlar ” and it should be “Yep Yeni Rakı Bunlar ”

    Do you have any idea about the problem?
    Thx

  7. Reva says:

    when i m using this code, it shows an error of
    “undefined local variable or method `export_person_url’ for ” can u please suggest a solution.

    • Brijesh shah says:

      Hi

      You need to define url in routes.rb set url according to your controller and action.

      Thanks
      Brijesh Shah

      • Reva says:

        Hi Brijesh
        I m new to rails.Im trying export the data in the table name abc to the excel sheet.My controller is abcs_controller.rb. Further shd i rename it as “export_abc_url”.shd i add post “/export_abc” to the routes. Kindly do help

  8. Reva says:

    Hi Brijesh
    I m new to rails.Im trying export the data in the table name abc to the excel sheet.My controller is abcs_controller.rb. Further shd i rename it as “export_abc_url”.shd i add post “/export_abc” to the routes. i m using rail3. i ve included in my routes. “match “export” => ‘abcs#export’
    resources :abcs” Kindly do help

    • Brijesh shah says:

      Hi

      set route as below:

      match "export" => "abcs#export" ,:as => :export_abcs
      

      or u can add in abcs resources if u had defined

      like

      map.resources :abcs  do 
      
      collection do
        get :export
      end
      
      end
      
  9. Reva says:

    Hi Brijesh.
    Thanks. I could overcome the error with your suggestion. i can open the export.html page successfully. when I click the link, it stays on the same page. where does the report.xls get generated? I m using IE7.

Leave a Reply

*

Panorama Theme by Themocracy