← Back to home

to_xls Plugin: Export to Excel in Rails the Easy Way

January 11th, 2009

Looking at the Google Analytics stats I realized that a lot of people are still reading my blog post about how to export to Excel in Rails.

IMO I prefer the csv method because it provides the same + more at no extra cost, but given people still wants xls files I wrote another rails plugin: to_xls.


UPDATE: SuperSaaS (Thanks!) mentioned that Excel does not interpret UTF8 characters in CSV files. So if that is a concern to you, maybe you should stick with XLS.

Using this plugin you don’t need the builder views, so it makes it as easy as the to_csv plugin I wrote. In fact it works the same.

In config/initializers/mime_types.rb register the custom mime type.


Mime::Type.register "application/vnd.ms-excel", :xls

In the controller where you want to export to excel, add the format.xls line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class UserController < ApplicationController

        def index
                @users = User.all

                respond_to do |format|
                        format.html
                        format.xml { render :xml => @users }
                        format.xls { send_data @users.to_xls }
                end
        end

        def show...
        def new...
        def edit...
        def create...
        def update...
        def destroy...

end

That’s it!

Install


./script/plugin install git://github.com/arydjmal/to_xls.git

← Back to home