Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (49 sloc) 2.64 KB

to_xls gem

This gem transform an Array or Hash into a excel file using the spreadsheet gem.


@users = User.all

@users.to_xls(:headers => false)
@users.to_xls(:columns => [:name, :role])
@users.to_xls(:columns => [:name, {:company => [:name, :address]}])
@users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => [:name, :company, :address])

When invoked on a Hash each value is output to a separate sheet named after the corresponding key. Options for each sheet can be specified using key/values in the options hash.

{ :users => @users, :posts => @posts }.to_xls
{ :users => @users, :posts => @posts }.to_xls(:users => { :columns => [:name, :role] }, :posts => { :columns => [:title, :content] })

If you want to use the file, for example in rails, you have two options. The first one is saving it first, and using send_file:

@users.to_xls.write '/path/to/file/users.xls'
send_file '/path/to/file/users.xls'

The second option is using the method to_xls_data and send_data

send_data @users.to_xls_data, :filename => 'users.xls'

The method to_xls_data accepts the same parameters as to_xls.


On rails, you might want to modify config/initializers/mime_types.rb and register a custom mime type there:

Mime::Type.register "application/", :xls

Example of use in Rails

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

class UserController < ApplicationController

  def index
    @users = User.all

    respond_to do |format|
      format.xml { render :xml => @users }
      format.xls { send_data @users.to_xls_data, :filename => 'users.xls' }


spreadsheet gem (automatically included if you require to_xls)

Installing on rails

Include next gems in your environment.rb config file:

  config.gem 'to_xls'

Contributing to to_xls

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


Copyright © 2011 Enrique García Cota. See LICENSE.txt for further details.