# encoding: utf-8 wb = xlsx_package.workbook wb.add_worksheet(name: "school_class") do |sheet| row = [] title = sheet.styles.add_style(:bg_color => "FFFF00",:border=>{ :style => :thin, :color => "666666" },:alignment=>{ :horizontal => :center,:vertical => :center ,:wrap_text => true}) column = sheet.styles.add_style(:alignment=>{ :horizontal => :center,:vertical => :center ,:wrap_text => true},:border=>{ :style => :thin, :color => "666666" }) @fields = [ "school_code", "school_name" , "school_address" ,"class_number","enrollment_limited","school_contact_person_name","department_job_title","office_tel_number","fax","mobile_number","email","enrollment"] @fields.each do |field| row << t('olympiamanagement.'+field) end #export data start export_data_rows = [] @OlympiaSchoolDataFields.each do |school_data_field| temp_row = [] @fields.each do |field_key| if field_key == "enrollment_limited" if school_data_field.enrollment_limited.to_i == 0 enrollment_limited = ClassSettingFields.all.select{|class_setting_field| class_setting_field.class_number_range[0] <= school_data_field.class_number && class_setting_field.class_number_range[1] >= school_data_field.class_number}.first.enrollment_available.to_s rescue ClassSettingFields.all.desc(:enrollment_available).first.enrollment_available.to_s else enrollment_limited = school_data_field.enrollment_limited end temp_row << enrollment_limited else temp_row << (school_data_field[field_key].to_s rescue '') end end export_data_rows << temp_row end #export data end sheet.add_row row , :style=> title,:widths => row.map{|cell| (cell.length* 11/ 2).round} , :types => :string export_data_rows.each do |infos_row| sheet.add_row infos_row , :style=> column , :types => :string end end