Exporting CSV and Excel File In ROR.

It’s very common now a days to export data from a Web application in CSV or Excel format and in this post i will show how to do that in a Rails application. 


Exporting to CSV:-

Step:1 /config/application.rb

require File.expand_path('../boot', __FILE__)

require 'csv'
require 'rails/all'


Step:2   /app/controllers/products_controller.rb


class ProductsController < ApplicationController
  def index
    @products = Product.order(:name)
    respond_to do |format|
      format.html
      format.csv { render text: @products.to_csv }
    end
  end
end

Step:3 /app/models/product.rb


class Product < ActiveRecord::Base
  attr_accessible :name, :price, :released_on
  
  def self.to_csv
    CSV.generate do |csv|
      csv << column_names
      all.each do |product|
        csv << product.attributes.values_at(*column_names)
      end
    end
  end
end

Step:4 /config/initializers/mime_types.rb

Mime::Type.register "application/xls", :xls

Step:5 /app/controllers/products_controller.rb


class ProductsController < ApplicationController
  def index
    @products = Product.order(:name)
    respond_to do |format|
      format.html
      format.csv { send_data @products.to_csv }
      format.xls { send_data @products.to_csv(col_sep: "\t") }
    end
  end
end

Step:6 /app/models/product.rb


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

Step:7 app/controllers/products.rb


class ProductsController < ApplicationController
  def index
    @products = Product.order(:name)
    respond_to do |format|
      format.html
      format.csv { send_data @products.to_csv }
      format.xls
    end
  end
end

Step:8 /app/views/products/index.xls.erb


<table border="1">
  <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Release Date</th>
    <th>Price</th>
  </tr>
  <% @products.each do |product| %>
  <tr>
    <td><%= product.id %></td>
    <td><%= product.name %></td>
    <td><%= product.released_on %></td>
    <td><%= product.price %></td>
  </tr>
  <% end %>
</table>

Step:9 /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 do |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>

Step:10 /app/views/products/index.html.erb


<h1>Products</h1>

<p>
  Download:
  <%= link_to "CSV", products_path(format: "csv") %> |
  <%= link_to "Excel", products_path(format: "xls") %>
</p>
<!-- Rest of file omitted -->

That's All !!

Comments

Popular posts from this blog

Push Notifications for IOS and Android In rails 4

Twilio apllication step by step process of SMS and Call API in rails