2017-02-16 125 views
-1

是否可以使用gem axlsx在xlsx中使用多個工作表導出。如果不是,請給我建議其他方式。Rails gem axlsx gem多張

我想在不同的工作表中輸出@search,@searchg,@clients,@overtimes的值。

@search = Operation.where(:date => params[:from_date].to_datetime..(params[:to_date].to_datetime + 1.day)) 
@searchg = Guard.where(:status => true).where(:induction_date => params[:from_date].to_datetime..(params[:to_date].to_datetime + 1.day)) 
@searcht = Guard.where(:status => false).where(:terminate_date => params[:from_date].to_datetime..(params[:to_date].to_datetime + 1.day)) 
@clients = Client.where(:status => false).where(:terminate_date => params[:from_date].to_datetime..(params[:to_date].to_datetime + 1.day)) 
@overtimes = Overtime.where(:date => params[:from_date].to_datetime..(params[:to_date].to_datetime + 1.day)) 
    respond_to do |format| 
     format.js 
     format.xlsx {render xlsx: 'report', filename: "Operation_reports_#{params[:from_date]}_#{params[:to_date]}.xlsx"} 
    end 

回答

1

是的,axlsx支持多張紙。事實上,你可以看到它在最先代碼片段在its readme

Axlsx::Package.new do |p| 
    p.workbook.add_worksheet(:name => "Pie Chart") do |sheet| 
    sheet.add_row ["Simple Pie Chart"] 
    %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] } 
    sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "example 3: Pie Chart") do |chart| 
     chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :colors => ['FF0000', '00FF00', '0000FF'] 
    end 
    end 
    p.serialize('simple.xlsx') 
end