我将文件对象传递给它。 控制器:
RejectLoadsUpload.import(params[:file])
型号:
def import(file)
parse_file(file)
end
def parse_file(filename)
#filename is absolute path
posts = parse_csv(filename)
posts.keep_if {|x| x.keys.include? "type"}
posts.each do |post|
record = build_record(post)
#Resque.enqueue(LoadPoster, record)
#ToTest this uncomment the following line and comment out the above line
RejectLoads.create(record)
end
@log.info "Finished parsing #{filename}..."
#File.delete(filename)
#if File.exists?(filename)
# @log.error "File #{filename} was not deleted successfully."
#end
end
# Takes in a file name that is a path to a CSV
# returns a posts which is an array of array of the parsed file
def parse_csv(filename)
workbook = Roo::Spreadsheet.open(filename.path.to_s, extension: :xlsx)
workbook.default_sheet = workbook.sheets[0]
header = workbook.row(1)
raise EdiLoadsFileEmpty if header.nil?
posts = []
(2..workbook.last_row).each do |i|
post = Hash[header.zip (workbook.row(i))]
posts.push post
end
posts
end
我试过了:
workbook = Roo::Spreadsheet.open(filename, extension: :xlsx)
workbook = Roo::Spreadsheet.open(filename.path, extension: :xlsx)
workbook = Roo::Spreadsheet.open(filename.tempfile.path.to_s, extension: :xlsx)
有无扩展名
也试图使用Roo::Excelx.new
同样没有运气
但是我得到一个参数错误:
header = workbook.row(1)
我认为错误是我打开文件的方式。
Started POST "/rejects/import" for 192.168.1.121 at 2015-07-28 13:41:36 -0500
Processing by RejectsController#import as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"8zv12AJ4p1cnnWv64c8/N/Nhh0TaMBNZaRzpBgMWcn0=", "file"=>#<ActionDispatch::Http::UploadedFile:0xb4cf00a8 @original_filename="EDIORD@REJ (4).xlsx", @content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"EDIORD@REJ (4).xlsx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n", @tempfile=#<File:/tmp/RackMultipart20150728-3506-18nzbha>>, "commit"=>"Import"}
Completed 500 Internal Server Error in 168ms
ArgumentError (ArgumentError):
app/models/service_objects/reject_loads_upload.rb:34:in `parse_csv'
app/models/service_objects/reject_loads_upload.rb:12:in `parse_file'
app/models/service_objects/reject_loads_upload.rb:7:in `import'
app/controllers/rejects_controller.rb:13:in `import'
Rendered /usr/local/rvm/gems/ruby-1.9.3-p551@loadmax/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.9ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p551@loadmax/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.5ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p551@loadmax/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.4ms)
在此处无记录:
workbook = Roo::Spreadsheet.open(filename, extension: :xlsx)
@log.info workbook
# => <#Roo::Excelx:517603520 @tmpdirs @tmpdir @filename @comments_files @rels_files @sheet_files @workbook @sheet_names @sheets @styles @shared_strings @sheets_by_name @options @cell @cell_type @cells_read @first_row @last_row @first_column @last_column @header_line>
workbook.default_sheet = workbook.sheets[0]
@log.info workbook.default_sheet
# => Sheet1
@log.info workbook.row(1) <--- Stack errors here now...
# => (Blank)
header = workbook.row(1)