rails excell 导入

excell导入:先把文件上传到路劲再直接读就行。需要gem  spreadsheet 在controller中调用以下migrate方法。(excell的表头对应when的内容)

  def migrate#导入信息
     require 'spreadsheet'
     Spreadsheet.client_encoding = 'UTF-8'
     book = Spreadsheet.open "#{RAILS_ROOT}/public/system/infos/#{self.id}/original/#{self.info_file_name}"
     @sheet1 = book.worksheet  0
     #@sheet2 = book.worksheet  1
     @sheet1.each 1 do |@row|
       each_do_row(@sheet1)
     end
     #@sheet2.each 1 do |@row|
       #each_do_row(@sheet2)
       # import_all_luan(@sheet2)
     #end
  end

  def each_do_row (sheet)
    if [email protected]?&&[email protected]?
      @student=Student.new
        for i in 0...sheet.row(1).size
            if @row[i]&&!@row[i].nil?                               
               case sheet[0,i]  
               when "学号"
                   @student.itemno=@row[i].to_s.split(".")[0]#学号
               when "姓名"
                   @student.name=@row[i]#姓名 
               end             
            end          
        end     
         @student.save    
         update_file=UpdateFile.find(self.id)
         update_file.update_attributes(:state=>1)                      
    end
  end




你可能感兴趣的:(excell导入)