Rails中导出excel的快速简便的方法

controller:
  def to_excele
    headers['Content-Type'] = "application/vnd.ms-excel"
    headers['Content-Disposition'] = 'attachment; filename="report.xls"'
    headers['Cache-Control'] = ''
    @posts = Post.find(:all)
    render :layout => false
  end


views:
<html>
  <head>
  <meta http-equiv="Content-Type" content="charset=utf-8" />
  <title>excele表格</title>
  </head>
  <body>
<table border="1">
  <tr><td>标题</td></tr>
  <%@posts.each do |post|%>
<tr>
  <td><%=post.title%></td>
</tr>
<% end %>
</table>
</body>
</html>


使用http header头来设置前端输出类型,要的就是这么简单。

你可能感兴趣的:(html,cache,Excel,Ruby,Rails)