Read more

Exporting to Excel from Rails without a gem

Henning Koch
March 17, 2015Software engineer at makandra GmbH

See this Railscast Show snapshot .

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show snapshot

Basically you can simply write views like index.xlsx.erb:

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  <Worksheet ss:Name="Sheet1">
        <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>
    <% @products.each do |product| %>
        <Cell><Data ss:Type="Number"><%= %></Data></Cell>
        <Cell><Data ss:Type="String"><%= %></Data></Cell>
        <Cell><Data ss:Type="String"><%= product.released_on %></Data></Cell>
        <Cell><Data ss:Type="Number"><%= product.price %></Data></Cell>
    <% end %>

This approach may help us overcome limitations in performance and styling that we encountered with the spreadsheet gem.

Posted by Henning Koch to makandra dev (2015-03-17 10:14)