1

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?

pkrawat1
  • 671
  • 7
  • 18
Jagdish Barabari
  • 2,673
  • 3
  • 20
  • 21

1 Answers1

1

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
pkrawat1
  • 671
  • 7
  • 18
  • @pkrawat1 this is an older post, but it just helped me a lot, so thanks for that. The one error I see in it and is probably what tripped up OP is that `file_path` should be `file.path`. Once I changed that, it worked like a charm. – nicholas79171 Jun 09 '15 at 12:43
  • @nicholas79171 Thanks for pointing that out. Glad it helped you. – pkrawat1 Jun 10 '15 at 07:19