add admin :download_excel

This commit is contained in:
EricTYL 2020-02-06 18:10:34 +08:00
parent 7fca57c5fb
commit c3cc50d293
4 changed files with 80 additions and 2 deletions

View File

@ -72,6 +72,17 @@ class Admin::ActivitiesController < OrbitMemberController
end
end
def download_excel
year_start = params[:year_start].to_i
year_end = params[:year_end].to_i
@data = get_data_for_excel(year_start,year_end)
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="activities.xlsx"'
}
end
end
private
def set_activity

View File

@ -18,4 +18,17 @@ module Admin::PersonalActivitiesHelper
finaldata << data
finaldata
end
def get_data_for_excel(year_start,year_end)
data = []
roles = Role.where(:disabled => false, :title.ne => "", :title.ne => nil).asc(:key)
roles.each do |role|
d = {}
d["name"] = role.title
mps = role.member_profile_ids
d["data"] = Activity.where(:year.gte => year_start, :year.lte => year_end, :member_profile_id.in => mps) rescue []
data << d
end
return data
end
end

View File

@ -0,0 +1,54 @@
# encoding: utf-8
wb = xlsx_package.workbook
@data.each_with_index do |role,idx|
data = role["data"]
wb.add_worksheet(name: role["name"] + "-" + idx.to_s) do |sheet|
heading = sheet.styles.add_style(:b => true, :locked => true)
row = ["Name"]
@site_in_use_locales.each do |locale|
row << t("personal_activity.activity_name") + " - " + t(locale.to_s)
end
@site_in_use_locales.each do |locale|
row << t("personal_activity.activity_organizer") + " - " + t(locale.to_s)
end
@site_in_use_locales.each do |locale|
row << t("personal_activity.activity_area") + " - " + t(locale.to_s)
end
row << t("personal_activity.year")
row << t("personal_activity.activity_start_date")
row << t("personal_activity.activity_end_date")
row << t("personal_activity.note")
sheet.add_row row, :style => heading
data.each do |activity|
row = [activity.member_profile.name]
@site_in_use_locales.each do |locale|
row << activity.activity_name_translations[locale.to_s]
end
@site_in_use_locales.each do |locale|
row << activity.activity_organizer_translations[locale.to_s]
end
@site_in_use_locales.each do |locale|
row << activity.activity_area_translations[locale.to_s]
end
row << activity.year
row << activity.activity_start_date
row << activity.activity_end_date
row << activity.note
sheet.add_row row
end
end
end

View File

@ -3,7 +3,7 @@ Rails.application.routes.draw do
scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do
get 'activities/download_excel_format' => 'activities#download_excel_format'
#post 'books/import_from_excel' => 'books#import_from_excel'
#post 'activities/import_from_excel' => 'activities#import_from_excel'
resources :activities do
collection do
@ -12,7 +12,7 @@ Rails.application.routes.draw do
# get 'toggle_hide' => 'books#toggle_hide'
get 'analysis'
get 'analysis_report'
# get "download_excel"
get 'download_excel'
end
end
end