我需要将我的模型导出到.xlsx并为axlsx模板提交一些参数(复选框)。
我有一个控制器“lead”和自定义帖子操作“export”,它应该调用我的export.xlsx.axlsx模板:
def export
respond_to do |format|
format.xlsx ## here is invoking my export.xlsx.axlsx
end
end
在我的routes.rb中,我有以下内容:
resources :leads do
collection { post :ipmort }
end
我正在尝试实现以下视图:
<%= form_tag export_leads_path do %>
<%= button_to "Export to Excel", {controller: 'leads', action: "export",
remote: true, form: { "data-type" => "xlsx" }} %>
<% end %>
但是当我点击这个按钮时,我有html数据格式,但不是xlsx
是否有可能在submit_tag,button_to?
中指定respond_to格式我尝试过使用&lt;%= link_to%&gt;但后来我无法得到我的形式参数..
<%= form_tag export_leads_path do %>
<%= link_to "Excel", export_leads_path(format: "xlsx", commit: "Excel"), method: :post %>
<% end %>
这里我没有lead_ids参数:
我也尝试了&lt;%submit_tag%&gt;,但结果与&lt;%= button_to%&gt;
相同请帮助我,我整天都在努力解决这个问题,并且不知道我还能做什么。如果您需要任何其他信息,请告诉我,我会尽快提供!
提前致谢!
答案 0 :(得分:1)
使用格式:“xls”
另外,您可以创建export.xls.erb。示例如下:
<% @headers = ["Id","Name","Email","Grade","Groups","% Total Playlist Videos Watched"] %>
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Users">
<Table>
<Row>
<% @headers.each do |column| %>
<Cell><Data ss:Type="String"><%=column%></Data></Cell>
<% end %>
</Row>
<% @users.each do |user| %>
<Row>
<Cell><Data ss:Type="Number"><%= user.id %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.name %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.email %></Data></Cell>
<Cell><Data ss:Type="Number"><%= user.grade %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.group_name %></Data></Cell>
<Cell><Data ss:Type="Number"><%= user.videos_seen %></Data></Cell>
</Row>
<% end %>
</Table>
</Worksheet>
</Workbook>
我希望它有所帮助。
答案 1 :(得分:1)
我找到了解决方案,这是我的提示。希望它对面临同样问题的人有所帮助。
1)路线:
resources :leads do
collection { post :export }
end
2)控制器操作:
def export
case params[:commit]
when "To Excel" then render xlsx: 'export'
when "To CSV" then send_data @leads.to_csv_modified(@lead_ids), filename: 'export.csv'
else render action: "index"
end
end
3)查看:
<%= form_tag export_leads_path, method: :post do %>
<%= text_field_tag :created_at_first %>
<%= text_field_tag :created_at_last%>
<% Lead.new.attributes.keys[0...Lead.new.attributes.keys.length-1].each_with_index do |field, index| %>
<%= check_box_tag "lead_ids[]", field, checked = true %>
<%= field %>
<% end %>
<%= submit_tag 'To Excel', controller: 'leads', action: "export" %>
<%= submit_tag 'To CSV', controller: 'leads', action: "export" %>
<% end %>
4)Axlsx模板(使用渲染xlsx:'export'在控制器中调用):
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'axlsx'
p = Axlsx::Package.new
wb = xlsx_package.workbook
style_shout = wb.styles.add_style sz: 16, b: true, alignment: { horizontal: :center }
wb.add_worksheet(name: "#{Time.now.to_date}") do |sheet|
sheet.add_row params[:lead_ids]
@leads.each_with_index do |lead, index|
sheet.add_row params[:lead_ids].map {|e| lead.send(e) }
end
end
5)方法to_csv_modified (在控制器中使用send_data @ leads.to_csv_modified(@lead_ids)调用,文件名:'import.csv'):
def self.to_csv_modified(lead_ids, options = {})
CSV.generate(options) do |csv|
csv << lead_ids
all.each do |lead|
csv << lead.attributes.values_at(*lead_ids)
end
end
end
如果您想了解它是如何工作的,请访问我的github conditional_exporting_to_XLSX_with_params
另一个有用的链接:Axlsx_documentation,Railscast #396 - importing csv-and-excel,Railscast #362 - exporting csv-and-excel