ruby on rails - How can I change my .xls.erb file to create a new sheet for each row? -


i following railscast tutorial exporting csv , excel. goal export excel. working fine except each product have on sheet. below code per tutorial , attempts @ adjusting needs.

/config/application.rb

require file.expand_path('../boot', __file__)  require 'csv' require 'rails/all' 

/config/initializers/mime_types.rb

mime::type.register "application/xls", :xls 

/app/models/product.rb

class product < activerecord::base   attr_accessible :name, :price, :released_on    def self.to_csv(options = {})     csv.generate(options) |csv|       csv << column_names       all.each |product|         csv << product.attributes.values_at(*column_names)       end     end   end end 

app/controllers/products.rb

class productscontroller < applicationcontroller   def index     @products = product.order(:name)     respond_to |format|       format.html       format.csv { send_data @products.to_csv }       format.xls     end   end end 

/app/views/products/index.xls.erb

<?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">id</data></cell>         <cell><data ss:type="string">name</data></cell>         <cell><data ss:type="string">release date</data></cell>         <cell><data ss:type="string">price</data></cell>       </row>     <% @products.each |product| %>       <row>         <cell><data ss:type="number"><%= product.id %></data></cell>         <cell><data ss:type="string"><%= product.name %></data></cell>         <cell><data ss:type="string"><%= product.released_on %></data></cell>         <cell><data ss:type="number"><%= product.price %></data></cell>       </row>     <% end %>     </table>   </worksheet> </workbook> 

all of works create excel sheet column of products , other columns attributes. attempt create individual worksheets each product, iterated through products above element in .xls.erb. led excel file being unreadable though.

/app/views/products/index.xls.erb

<?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">  <% @products.each |product| %>   <worksheet ss:name="sheet1">     <table>       <row>         <cell><data ss:type="string">id</data></cell>         <cell><data ss:type="string">name</data></cell>         <cell><data ss:type="string">release date</data></cell>         <cell><data ss:type="string">price</data></cell>       </row>       <row>         <cell><data ss:type="number"><%= product.id %></data></cell>         <cell><data ss:type="string"><%= product.name %></data></cell>         <cell><data ss:type="string"><%= product.released_on %></data></cell>         <cell><data ss:type="number"><%= product.price %></data></cell>       </row>     </table>   </worksheet>  <% end %> </workbook> 

how can change .xls.erb file create new sheet each product row?


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -