1

我正在使用 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?

4

1 回答 1

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
于 2015-02-10T17:45:41.237 回答