Rails: Excel columns, seeds -
i created excel sheet columns id, task, description
i find , set in seeds.rb:
require 'spreadsheet' spreadsheet.open('/db/data/uebung.xls') |uebung| book.worksheet('sheet1').each |row| break if row[0].nil? puts row.join(',') end end
i message:
[deprecated] requiring 'parseexcel', 'parseexcel/parseexcel' and/or 'parseexcel/parser' loading compatibility layer provides drop-in replacement parseexcel library. code makes reading of spreadsheet documents less efficient , removed in spreadsheet version 1.0.0
what think. is not easy import excel data?
i utilize roo import excel rails app. , not hard that. can watch the railscasts ryan bates roo if want.
i give illustration code of roo
gemfile
gem 'rubyzip' gem 'spreadsheet' gem 'nokogiri' gem 'roo'
controller:
def import @trainerone = trainerone.find(params[:trainerone_id]) trainerone.import(params[:file], @trainerone.id) redirect_to @trainerone, notice: "import success." end
model:
def self.import(file, trainer_id) trainerone = trainerone.find(trainer_id) spreadsheet = open_spreadsheet(file) header = spreadsheet.row(1) (2..spreadsheet.last_row).each |i| row = hash[[header, spreadsheet.row(i)].transpose] trainer = trainerone.create! row.to_hash end end def self.open_spreadsheet(file) case file.extname(file.original_filename) when '.csv' csv.new(file.path, nil, :ignore) when '.xls' excel.new(file.path, nil, :ignore) when '.xlsx' excelx.new(file.path, nil, :ignore) else raise "unknown file type: #{file.original_filename}" end end
view:
<%= form_tag import_trainerones_path(@trainerone.id), multipart: true %> <%= file_field_tag :file %> <%= submit_tag "import" %> <% end %>
this import_trainerones_path
taken route
pointing @ action: import
on trainerone controller
(look @ controller above)
hope can help you..!
ruby-on-rails excel
No comments:
Post a Comment