def self.import_xls(file) spreadsheet = open_spreadsheet(file) header = spreadsheet.row(18) (19..spreadsheet.last_row).each do |i| row = Hash[[header, spreadsheet.row(i)].transpose] order = find_by_id(row["id"]) || new order.attributes = row.to_hash.slice(*accessible_attributes) order.save! end end
def self.open_spreadsheet(file) case File.extname(file.original_filename) when ".csv" then Roo::CSV.new(file.path, file.original_filename) when ".xls" then Roo::Excel.new(file.path, nil) when ".xlsx" then Roo::Excelx.new(file.path, nil) else raise "未知类型:#{file.original_filename}" end end
上传的无论是.csv 还是.xls 都是报这个错误
测试时报 uninitialized constant OpenofficeExcel 和 uninitialized constant Excelx
TypeError in OrdersController#import_xls
C:/Users/LHP/AppData/Local/Temp/RackMultipart20140106-4700-sarris is not an Excel file
Rails.root: F:/projects/quality_discount Application Trace | Framework Trace | Full Trace
app/models/order.rb:256:in new'
app/models/order.rb:256:in
open_spreadsheet'
app/models/order.rb:243:in import_xls'
app/controllers/orders_controller.rb:55:in
import_xls'
Request
Parameters:
{"utf8"=>"✓", "authenticity_token"=>"Io2Iq3bsBE4XNuTdcBXFwmD/8rWN32tJZ8OI/ZQNYk8=", "file"=>#>, "commit"=>"导入"}