0

How to read attached excel files with roo gem

My view is

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

I tried

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

But I receive an error *** IOError Exception: file /tmp/RackMultipart20130921-4175-165dgyw/hotel_information.xlsx does not exist and edit the code and tried

   Roo::Excelx.new(xlsxFile.path)

get the error

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

2 回答 2

3

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

Roo::Excelx.new(xlsxFile.path, file_warning: :ignore))
于 2014-03-12T12:45:17.123 回答
0

我从这里得到了解决方案http://railscasts.com/episodes/396-importing-csv-and-excel?view=asciicast

于 2013-09-21T16:58:41.327 回答