简体   繁体   English

读取xlsx / xls文件-Rails 4

[英]Read a xlsx/xls file - Rails 4

Good day stackoverflow 美好的一天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 问题在于它可以读取Spreadsheet gem上的.xls文件,但无法读取.xlsx文件

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 与此相反,小豆宝石可以读取的.xls / .XLSX文件,但帕拉姆file_data.path没有为工作Excel.new功能,显示"is not an Excel file"错误

I send the excel file in a file_field_tag in a form on the view, of course 我当然以视图的形式在file_field_tag中发送excel文件

Any ideas for the param file_data.path stackoverflow? 对参数file_data.path stackoverflow有任何想法吗?

When you use roo gem to read xlsx file, use Excelx class 当您使用roo gem读取xlsx文件时,请使用Excelx

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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