Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
This gem transform an Array into a excel file using the spreadsheet gem.
branch: master

This branch is 74 commits behind splendeo:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.document
.gitignore
.rspec
Gemfile
LICENSE.txt
README.rdoc
Rakefile
VERSION
init.rb

README.rdoc

to_xls gem

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

Usage

@users = User.all

@users.to_xls
@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.

Requirements

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

Mime::Type.register "application/vnd.ms-excel", :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.html
      format.xml { render :xml => @users }
      format.xls { send_data @users.to_xls_data, :filename => 'users.xls' }
    end
  end
end

Dependencies

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

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

Something went wrong with that request. Please try again.