rails export search results as excel by spreadsheet

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

    require 'spreadsheet'
     # to create a spreadsheet file
      @selectedevent=Event.find_by_id(params[:id])
        Spreadsheet.client_encoding='UTF-8'
        book = Spreadsheet::Workbook.new
        sheet1 = book.create_worksheet
        sheet1.name = 'attendees of event'
        # to set data
        sheet1.row(0).concat %w{EventTitle EventDate  FullName Email RegisteredDate}
        #resultData=Array.new
        rowno=1
        @selectedevent.students.each do |student|
            sheet1.row(rowno).push @selectedevent.title_fr
             sheet1.row(rowno).push @selectedevent.event_date.strftime("%Y-%m-%d") 
             sheet1.row(rowno).push User.find_by_id(student.user_id).full_name_fr
             sheet1.row(rowno).push student.login
             sheet1.row(rowno).push Attendee.find_by_event_id_and_student_id(@selectedevent.id,student.id).registered_date.strftime("%Y-%m-%d") 
            rowno +=1
         end
        # to set the format 
        sheet1.row(0).height = 12
        format = Spreadsheet::Format.new :color => :blue,
                                        :weight => :bold,
                                        :size => 12
        sheet1.row(0).default_format = format  
       # To write and send file
       
        filepath=Rails.root+"public/excel_results/#{@selectedevent.title_fr}-attendees list---  #{Time.now}.xls"
        book.write filepath
        send_file filepath