Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
This gem transform an Array into a excel file using the spreadsheet gem.

This branch is 16 commits ahead, 12 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
MIT-LICENSE.txt
README.rdoc
Rakefile
init.rb
to_xls.gemspec

README.rdoc

to_xls gem

This gem transform an Enumeration (i.e. an Array) into an excel file using the spreadsheet gem.

It is (very slightly) oriented towards exporting ActiveRecord sets, but it can be used outside of Rails as well.

This project is a fork of [github.com/splendeo/to_xls]. It adds the possibility of specify the column format and column width.

Usage

@users = User.all

@users.to_xls                        # by default, it exports using the "attributes" field on the first item of the collection
@users.to_xls(:name => "Users")      # specifies the Sheet name (by default Sheet1)
@users.to_xls(:headers => false)     # don't include headers
@users.to_xls(:columns => [:name, :role]) # include only these columns, on this order
@users.to_xls(:columns => [:name, {:company => [:name, :address]}]) # able to pick associations/called methods
@users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => ['Name', 'Company', 'Address']) # provide better names for the associated columns

Format

This gem includes a simplified way to change the format of the cells. Also this gem provides a simplified way for changing the format of the headers.

To change the format of the cells, headers and columns the code should be like this:

User.all.to_xls :header_format => {:weight => :bold, :color => :red}, 
  :column_format => {[:created_at, :updated_at] => {:number_format => 'DD/MM/YYYY'}, :all => {:horizontal_align => :center}}, 
  :column_width => {:all => 5, [:created_at, :updated_at] => 12}

Check the wiki page for a more detailed example using column_format an column_width

To have a full list of the format admited for the cells and headers you may have a look on the source code of spreadsheet gem. This gem runs under to_xls, so the format supported by this gem are the ones of spreadsheet.

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, :filename => 'users.xls' }
    end
  end
end

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

Dependencies

  • spreadsheet gem (automatically included if you require to_xls)

Installing on rails 3.x

In your Gemfile

gem 'to_xls', :git => 'git://github.com/LogicalBricks/to_xls.git'

Then execute

bundle install

ToXml::Writer

You can export to a file or spreadsheet book using the internal ToXml::Writer class:

ToXls::Writer.new(users, :name => 'Users').write_io(file) # writes to a given file
ToXls::Writer.new(users, :name => 'Users').write_book(book) # writes to a spreadsheet book (adds a new sheet)

The options of Writer.new(array, options) are the same as in 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 Splendeo Innovación. See MIT-LICENSE.txt for further details.

Credits

  • Ian Link & mlecho helped implementing the formats

Changelog

1.5.1

  • to_xls now extends Enumerable instead of Array

  • Included :cell_format and :header_format options

  • Included :column_format and :column_width options

  • Used bundler instead of jeweller

Something went wrong with that request. Please try again.