Skip to content

prawin/to_xls-rails

 
 

Repository files navigation

to_xls plugin

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.

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
#

@posts.to_xls
@posts.to_xls(:only => [:title, :body])
@posts.to_xls(:except => [:id])
@posts.to_xls(:header => false)

# Translation of a column-value
@posts.to_xls{|column, value| column==:salutation ? t(value) : 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)
      #}
    end
  end
end

Dependencies

gem install spreadsheet

Install

with gem
gem install to_xls-rails

with bundler
gem 'to_xls-rails'
bundle install

with git
git clone http://github.com/liangwenke/to_xls-rails.git

Note

Copyright © 2010 liangwenke.com@gmail.com, released under the MIT license

About

Export to excel file

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%