我有以下动作:
def users_report
@users = Kid.where(confirmation_token: nil).paginate(:page => params[:page], :per_page => 30)
respond_to do |format|
format.html
format.xls { send_data @users.to_csv(col_sep: "\t") }
end
end
我也有两个视图“ users_report.html.haml”和“ users_report.xls.erb”,但是当我按下按钮导出到excel时,我得到了文件users_report.xls,但是我只能看到对象,但是users_report.xls .erb具有他的结构,所有字段均按列排序。
有人可以在这里帮我吗?
提前致谢
更新XLS视图
<?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="Sheet1">
<Table>
<Row>
<Cell><Data ss:Type="String">Name</Data></Cell>
<Cell><Data ss:Type="String">Surname</Data></Cell>
<Cell><Data ss:Type="String">Email</Data></Cell>
<Cell><Data ss:Type="String">Age</Data></Cell>
<Cell><Data ss:Type="String">School</Data></Cell>
<Cell><Data ss:Type="String">Class</Data></Cell>
<Cell><Data ss:Type="String">Native Language</Data></Cell>
<Cell><Data ss:Type="String">Practised Language</Data></Cell>
<Cell><Data ss:Type="String">Conversations same Native</Data></Cell>
<Cell><Data ss:Type="String">Convserations different Native</Data></Cell>
<Cell><Data ss:Type="String">Message same Native</Data></Cell>
<Cell><Data ss:Type="String">Message different Native</Data></Cell>
<Cell><Data ss:Type="String">Posts</Data></Cell>
<Cell><Data ss:Type="String">Videos watched</Data></Cell>
<Cell><Data ss:Type="String">Clossed/Finished calls</Data></Cell>
<Cell><Data ss:Type="String">Missed calls</Data></Cell>
<Cell><Data ss:Type="String">Connections per Week</Data></Cell>
<Cell><Data ss:Type="String">Nb of foreign friends</Data></Cell>
<Cell><Data ss:Type="String">Nb of friends same country</Data></Cell>
<Cell><Data ss:Type="String">Activation Date</Data></Cell>
<Cell><Data ss:Type="String">Email Parent</Data></Cell>
<Cell><Data ss:Type="String">Parent Activated</Data></Cell>
</Row>
<% @users.each do |user| %>
<Row>
<Cell><Data ss:Type="String"><%= user.name rescue _("No name") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.surname rescue _("No surname") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.email rescue _("No email") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.age rescue _("No age") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.school.name rescue _("No School") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.courses.first.name rescue _("No Course") %></Data></Cell>
<Cell><Data ss:Type="String"><%= sentence_native_languages_of user rescue _("No native language") %></Data></Cell>
<Cell><Data ss:Type="String"><%= sentence_practise_languages_of user rescue _("No practise language") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_native_conversations rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_foreign_conversations rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_native_messages rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_foreign_messages rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_activity_posts rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_finished_closed_calls rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_missed_calls rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= number_with_precision(user.avg_of_connections_week, precision: 3) rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_foreign_friends rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.number_of_friends_same_country rescue _("0") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.confirmed_at.try(:strftime, "%d/%m/%Y") %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.tutor.email rescue _('No parent email') %></Data></Cell>
<Cell><Data ss:Type="String"><%= user.tutor.confirmed? rescue _('No parent email') %></Data></Cell>
</Row>
<% end %>
</Table>
</Worksheet>
</Workbook>
将您的操作更改为类似以下内容:
format.xls { send_data Kid.to_csv({col_sep: "\t"}, @users) }
您不必更改任何视图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句