如何使用roo gem读取附件

时间:2013-09-21 15:23:34

标签: ruby-on-rails ruby ruby-on-rails-3

如何使用roo gem

阅读附加的Excel文件

我的观点是

<%= form_for @hotel, :url => { :action => "create_by_excel_sheet" } do |f| %>
  <%= f.file_field :excel_sheet %>
   <%= f.submit %>
<%end%>

我试过

def create_by_excel_sheet
    xlsxFile = params[:hotel][:excel_sheet]
    prefix_tmp_path = xlsxFile.path
    filename = xlsxFile.original_filename
    fullname = File.join(prefix_tmp_path,filename) 

    s = Roo::Excelx.new(fullname)

    for i in 1..14
      puts s.cell(i,3) 
    end
end

但是我收到错误*** IOError Exception: file /tmp/RackMultipart20130921-4175-165dgyw/hotel_information.xlsx does not exist并编辑了代码并尝试了

   Roo::Excelx.new(xlsxFile.path)

得到错误

TypeError Exception: /tmp/RackMultipart20130921-4175-165dgyw is not an Excel-xlsx file

2 个答案:

答案 0 :(得分:3)

由于您知道它是xlsx文件,因此您可以添加此选项以忽略文件警告和异常:

Roo::Excelx.new(xlsxFile.path, file_warning: :ignore))

答案 1 :(得分:0)