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: 8879787e0983fd…
Pull request Compare This branch is even with splendeo:8879787e0983fdced22101d1be50b84023667421.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
MIT-LICENSE
README.rdoc
init.rb

README.rdoc

to_xls plugin

This Rails plugin is the easiest way to export to Excel. It gives you the ability to call to_xls to a collection of activerecords (Array). The builder options are the same as to_json / to_xml, except for the :include.

NOTE: IMO I would use the to_csv plugin; it's more compatible. (github.com/arydjmal/to_csv)

Compatibility

Works only for MS Excel 2003+

Usage

@users = User.all

#
# defaults are export headers and all fields
#

@users.to_xls
@users.to_xls(:only => [:last_name, :role])
@users.to_xls(:headers => false)
@users.to_xls(:except => [:last_name, :role])
@users.to_xls(:except => :role, :methods => :admin?)

Real life example

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.

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

Dependencies

None.

Install

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

Note

Does not work on a single activerecord, ie, User.first.to_xls.

Copyright © 2009 Ary Djmal, released under the MIT license

Something went wrong with that request. Please try again.