在Rails 4中导入xls文件不能正常工作Roo :: Excel.new

时间:2015-02-10 17:34:35

标签: ruby-on-rails ruby ruby-on-rails-4 import-from-excel roo

我使用rails 4以下代码导入.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

获得以下错误

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

有谁能告诉我如何导入.xls?

1 个答案:

答案 0 :(得分:1)

试试这个

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