Skip to content

Commit

Permalink
added to_xls_data method
Browse files Browse the repository at this point in the history
  • Loading branch information
Splendeo committed May 18, 2010
1 parent 305c9dc commit 4527a9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
26 changes: 11 additions & 15 deletions README.rdoc
Expand Up @@ -6,16 +6,22 @@ This gem transform an Array into a excel file using the spreadsheet gem.

@users = User.all

#
# defaults are export headers and all fields
#

@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])

In order to send a file from the controller, you can save it on your server first:

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

Alternatively you can use 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

Expand All @@ -35,29 +41,19 @@ In the controller where you want to export to excel, add the format.xls line.
respond_to do |format|
format.html
format.xml { render :xml => @users }
format.xls { send_data @users.to_xls }
format.xls { send_data @users.to_xls_data, :filename => 'users.xls' }
end
end

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

end


== Dependencies

spreadsheet gem


== Install

Include next gems in your environment.rb config file:

config.gem 'spreadsheet'
config.gem 'to_xls'

7 changes: 7 additions & 0 deletions lib/to_xls.rb
@@ -1,5 +1,6 @@
require 'rubygems'
require 'spreadsheet'
require 'stringio'

class Array
# Options for to_xls: columns, name, header
Expand Down Expand Up @@ -36,6 +37,12 @@ def to_xls(options = {})
return book
end

def to_xls_data(options = {})
data = StringIO.new('')
self.to_xls(options).write(data)
return data.string
end

private
def aux_to_xls(item, column, row)
if column.is_a?(String) or column.is_a?(Symbol)
Expand Down

0 comments on commit 4527a9b

Please sign in to comment.