Giter VIP home page Giter VIP logo

bom-d-van / to_xls_extension Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arydjmal/to_xls

1.0 1.0 0.0 200 KB

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. The builder options are the same as to_json / to_xml, except for the :include.

Home Page: http://arydjmal.com/2009/1/11/to_xls-plugin-export-to-excel-in-rails-the-easy-way

License: MIT License

Ruby 100.00%

to_xls_extension's Introduction

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)

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(:methods => :admin?)
@users.to_xls(:headers_mapping => { :last_name => "family name", :address => "living place" })

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

to_xls_extension's People

Contributors

arydjmal avatar bom-d-van avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.