Skip to content

mitigate-dev/xport

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Xport

Build Status Code Climate

Tabular data export to Excel, CSV, etc.

Features

  • Header groups - column :name, group: :project
  • Column widths - column :name, width: 10
  • Column header titles - column :name, header: "Full name"
  • Column types - column :name, type: :string
  • Column styles - column :pct, style: { num_fmt: Axlsx::NUM_FMT_PERCENT, format_code: '0.0%' }
  • Cell colors - cell.color = "AAAAAA"
  • Cell comments - cell.comment = "..."

Formatters

Feature csv axlsx rubyXL xlsxtream
Column groups No Yes Yes No
Column widths No Yes Yes No
Column types No Yes No* No
Column styles No Yes No* No
Cell colors No Yes No* No
Cell comments No Yes No* No

* - PRs are welcome

Installation

Add this line to your application's Gemfile:

gem 'xport'
gem 'axlsx' # optional
gem 'rubyXL' # optional
gem 'xlsxtream' # optional

And then execute:

$ bundle

Usage

class User < ActiveRecord::Base; end
User.create(name: "John")
User.create(name: "Ben")

class UserExport < Xport::Export
  include Xport::CSV
  include Xport::Axlsx

  columns do
    column :id
    column :name, header: "Full name" do |user|
      user.name.upcase
    end
    column :email do |user|
      cell = Xport::Cell.new
      cell.value = "#{user.id}@example.com"
      cell.color = "AAAAAA"
      cell.comment = "Excel comment"
      cell
    end
  end
end

UserExport.new(User.all).to_csv
UserExport.new(User.all).to_xlsx

Output:

id,Full name,email
1,JOHN,1@example.com
2,BEN,2@example.com

See examples for more examples.

Header groups

class UserExport < Xport::Export
  include Xport::Axlsx

  columns do
    column(:id,    group: "User")
    column(:name,  group: "User")
    column(:email, group: "User")
    column(:admin, group: "Roles") { |u| "No" }
    column(:owner, group: "Roles") { |u| "Yes" }
  end
end

File.open("export.xlsx", "wb") do |f|
  f.write UserExport.new(users).to_xlsx.read
end

Output:

Excel screenshot

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mak-it/xport. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.