2010-04-29 150 views
3

我想將頁面信息導出到Excel,誰可以告訴我怎麼做到這一點? 謝謝!在軌道上導出Excel紅寶石

+0

對於它的價值,我檢查了幾個選項ŧ o做到這一點,並使用電子表格寶石(http://rubygems.org/gems/spreadsheet)結束。這個文檔有點兒,但它非常強大,我能夠擴展它以適應我的需求。 – 2011-04-17 21:02:35

+0

請谷歌下次第一次(例如'導出爲ex​​cel的導軌'給你與你在評論部分給你的相同頁面)) – mrmnmly 2012-06-12 14:37:04

回答

9

像這樣的東西可能會有所幫助:

http://blog.dhavalparikh.co.in/2009/04/export-to-excel-in-ruby-on-rails/

控制器

class UserController < ApplicationController 
    def export 
    headers['Content-Type'] = "application/vnd.ms-excel" 
    headers['Content-Disposition'] = 'attachment; filename="report.xls"' 
    headers['Cache-Control'] = '' 
    @users = User.find(:all) 
    end 

查看

export.html.erb 

<%= link_to "Export as Excel", export_person_url %> 

_report.html.erb 

<table border="1"> 
    <tr> 
    <th>Name</th> 
    </tr> 
    <% @users.each do |u| %> 
    <tr> 
    <td><%= u.name %></td> 
    <% end %> 
</tr> 
</table> 
+0

謝謝你的幫助!現在我使用電子表格插件來解決問題 – 2010-04-29 10:03:53

+0

使用此解決方案時出現編碼錯誤 – 2012-07-23 19:40:23