在rails中没有这样的文件或目录@ rb_sysopen - /tmp/oo_20161031-6508-1bswvam/roo_workbook.xml

时间:2016-10-31 06:15:02

标签: ruby-on-rails excel

我正在使用gem roo 。我在这里收到错误

  

没有这样的文件或目录@ rb_sysopen -   /tmp/oo_20161031-6508-1bswvam/roo_workbook.xml

我的模型编码是,

  def self.import(file)
spreadsheet = Roo::Spreadsheet.open(file.path)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
  row = Hash[[header, spreadsheet.row(i)].transpose]
  subject = find_by(id: row["id"]) || new
  subject.attributes = row.to_hash
  subject.save!
end

结束

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)
  when ".xml" then Roo::XML.new(file.path, nil, :ignore)
else raise "Unknown file type: #{file.original_filename}"
end

结束

它将参数设为,

"utf8"=>"✓", "authenticity_token"=>"mk8vcsZQSd0YvjxM1EOQB8IYlyf6hDgAC/FE8lgGDElbkaPIjtiCNGEynsjfh1KklHErjp+aWWuDzVOZah8PVA==", "file"=>#<ActionDispatch::Http::UploadedFile:0x0055a570f4f8d8 @tempfile=#<Tempfile:/tmp/RackMultipart20161031-6508-18g6ask.xlsx>, @original_filename="TEST.xlsx", @content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"TEST.xlsx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n">, "commit"=>"Import"

错误表示打开文件格式。对此有什么正确的解决方案 感谢。

1 个答案:

答案 0 :(得分:0)

当excel文件保存错误时会出现此错误。这使得谜题,由应用程序打开文件。因此,检查excel保存或路径或版本,是此问题的解决方案。