Welcome to xls_porter¶ ↑
Simple data export to XLS and import from XLS to ActiveRecord model. The source is available at github.com/mr5080att/xls_porter.git.
Dependencies¶ ↑
-
CarrierWave for file uploading.
-
Ruby Spreadsheet.
Mixins: Extending a class.¶ ↑
class MyXlsPorter extend XlsPorter end MyXlsPorter.to_xls(User.all, %w(id name first_name last_name), "Users")
Mixins: Including in a class.¶ ↑
file: app/controllers/application_controller.rb class ApplicationController < ActionController::Base include XlsPorter end
Instances of the classes that inherits from ApplicationController will have access to xls_porter methods.
file: app/controllers/networks_controller.rb class NetworkController < ApplicationController # /networks/xls_download def xls_download model_to_xls(Network).write(stream = StringIO.new) send_data(stream.string, :filename => "#{Network.name.pluralize}.xls", :type => "Application/Excel") end # /networks/xls_upload def xls_upload @model_updates = xls_upload_and_update_model(params[:data_file], Network) render "uploaded/uploaded_updates" end end
The uploaded data file is stored in “tmp/upload” temporarily and deleted after the method xls_upload_and_update_model completes successfully.