Jagdish Barabari
Jagdish Barabari

Reputation: 2703

Import xls file in Rails 4 not working Roo::Excel.new

I am using rails 4 the following code to import .xls:

def self.open_spreadsheet(file)
  case File.extname(file.original_filename)
    when ".csv" then Roo::Csv.new(file.path, nil, :ignore)
    when '.xls' then Roo::Excel.new(file.path, nil, :ignore)
    when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
    else raise "Unknown file type: #{file.original_filename}"
  end
end

Getting the following error

Supplying `packed` or `file_warning` as separate arguments to
`Roo::Excel.new` is deprecated. Use an options hash
instead.Started GET "/members"

Can anyone tell me how to import .xls?

Upvotes: 1

Views: 1409

Answers (1)

pkrawat1
pkrawat1

Reputation: 681

Try This

def self.get_file_type(file)
  File.extname(file.original_filename).gsub('.','')
end

def self.open_spreadsheet(file)
  extension = get_file_type(file)
  if extension.in?(%w(csv xls xlsx))
    Roo::Spreadsheet.open(file.path, extension: extension)
  else
    raise "Unknown file type: #{file.original_filename}"
  end
end

Upvotes: 1

Related Questions