我使用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?
答案 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