README.rdoc in to_xls-rails-1.1.1 vs README.rdoc in to_xls-rails-1.1.2
- old
+ new
@@ -1,62 +1,79 @@
-== to_xls plugin
+== to_xls-rails
-This simple plugin gives you the ability to call to_xls to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.
+This simple plugin gives you the ability to call to_xls to a collection of activerecords for Rails.
+== Install
+ gem install spreadsheet
+ gem install to_xls-rails
+
+
== Usage
add below to RAILS_ROOT/config/initializers/mime_types.rb
Mime::Type.register_alias "text/excel", :xls
-
+
@posts = Post.all
-
+
#
- # default are export header and all fileds
+ # default are export header and all fileds
#
-
+
@posts.to_xls
@posts.to_xls(:only => [:title, :body])
@posts.to_xls(:except => [:id])
@posts.to_xls(:header => false)
+ # Will prepend above header
+ # | Col 0, Row 0 | Col 1, Row 0 |
+ # | Col 0, Row 1 | |
+ @posts.to_xls(:prepend => [["Col 0, Row 0", "Col 1, Row 0"], ["Col 0, Row 1"]])
+
# Translation of a column-value
- @posts.to_xls{|column, value| column==:salutation ? t(value) : value}
+ @posts.to_xls{|column, value, row_index| column==:salutation ? t(value) + " at #{row_index}" : value}
== Example
class PostsController < ApplicationController
def index
@posts = Post.all
-
+
respond_to do |format|
format.xls { send_data(@posts.to_xls) }
- #format.xls {
- # filename = "posts-#{Time.now.strftime("%Y%m%d%H%M%S")}.xls"
- # send_data(@posts.to_xls, :type => "application/excel; charset=utf-8; header=present", :filename => filename)
- #}
+ # format.xls {
+ # filename = "posts-#{Time.now.strftime("%Y%m%d%H%M%S")}.xls"
+ # send_data(@posts.to_xls, :type => "application/excel; charset=utf-8; header=present", :filename => filename)
+ # }
end
end
end
-== Dependencies
+ Works on Rails 3:
- gem install spreadsheet
-
+ class PostsController < ApplicationController
+ def index
+ # add this to config/initializers/mime_types.rb
+ Mime::Type.register "application/vnd.ms-excel", :xls
-== Install
+ @posts = Post.all
- with gem
- gem install to_xls-rails
+ respond_to do |format|
+ format.xls {
+ send_data @posts.to_xls
+ return # prevet Rails to seek for index.xls.erb
+ }
+ end
+ end
+ end
- with bundler
- gem 'to_xls-rails'
- bundle install
- with git
- git clone http://github.com/liangwenke/to_xls-rails.git
+== Contributors
+
+* {Juan Pablo Marzetti}[http://github.com/yonpols] Added Support for Value-Substitution
+* {Denis Yagofarov}[http://github.com/denyago] Added prepend some data above header and access current array element index in &block
== Note
Copyright (c) 2010 liangwenke.com@gmail.com, released under the MIT license