简体   繁体   中英

Read a xlsx/xls file - Rails 4

Good day stackoverflow

I have this in the controller

def upload
 file_data = params[:data]

 #s = Roo::Excel.new(file_data.path)
 book = Spreadsheet.open file_data.path

 #--- continuous to read the file ---#
end

The problem is that it can read a .xls file on the Spreadsheet gem, but can't read a .xlsx file

In contrast the Roo gem can read both .xls/.xlsx files, but the param file_data.path don't work for the Excel.new function, shows a "is not an Excel file" error

I send the excel file in a file_field_tag in a form on the view, of course

Any ideas for the param file_data.path stackoverflow?

When you use roo gem to read xlsx file, use Excelx class

   if File.extname(file_data.path) == ".xlsx"
    s = Roo::Excelx.new(file_data.path)
   else
    s = Roo::Excel.new(file_data.path)
   end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM