In controller need to add the import method with background job.
def import
new_filename = "#{File.basename(params[:file].original_filename)}_new"
new_filepath = File.join(Dir.tmpdir, new_filename)
FileUtils.cp(params[:file].path, new_filepath)
BackJob.perform_later(new_filepath,
File.extname(params[:file].original_filename))
redirect_to root_url, notice: 'It take sometime to import'
rescue StandardError => e puts e.inspect
end
In view index.html.erb add the import button with files.
<%= form_tag import_lists_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
In BackJob.rb add the import method for storing the data from xls
class BackJob < ApplicationJob
queue_as :default
def perform(fpath, file)
import(fpath, file)
end
def import(filepath, extname)
spreadsheet = open_spreadsheet(filepath, extname)
columns = %w[column_one column_tow ]
(2..spreadsheet.last_row).each do |i|
row = Hash[[db_columns, spreadsheet.row(i)].transpose]
obj = ModelName.new
obj.attributes = row.to_hash.slice(*columns)
obj.save
end
end
def open_spreadsheet(filepath, extname)
case extname
when '.csv' Roo::Csv.new(filepath)
when '.xls' Roo::Excel.new(filepath)
when '.xlsx' Roo::Excelx.new(filepath, file_warning: :ignore)
else raise 'Unknown file type: extname'
end
end
end
def import
new_filename = "#{File.basename(params[:file].original_filename)}_new"
new_filepath = File.join(Dir.tmpdir, new_filename)
FileUtils.cp(params[:file].path, new_filepath)
BackJob.perform_later(new_filepath,
File.extname(params[:file].original_filename))
redirect_to root_url, notice: 'It take sometime to import'
rescue StandardError => e puts e.inspect
end
In view index.html.erb add the import button with files.
<%= form_tag import_lists_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import" %>
<% end %>
In BackJob.rb add the import method for storing the data from xls
class BackJob < ApplicationJob
queue_as :default
def perform(fpath, file)
import(fpath, file)
end
def import(filepath, extname)
spreadsheet = open_spreadsheet(filepath, extname)
columns = %w[column_one column_tow ]
(2..spreadsheet.last_row).each do |i|
row = Hash[[db_columns, spreadsheet.row(i)].transpose]
obj = ModelName.new
obj.attributes = row.to_hash.slice(*columns)
obj.save
end
end
def open_spreadsheet(filepath, extname)
case extname
when '.csv' Roo::Csv.new(filepath)
when '.xls' Roo::Excel.new(filepath)
when '.xlsx' Roo::Excelx.new(filepath, file_warning: :ignore)
else raise 'Unknown file type: extname'
end
end
end