Friday, August 26, 2016

Export Data in xls format

Step 1:

Add Gem into Gemfile

gem 'to_xls'


Step 2:


app/admin/view/stories/index.html.haml


Give link on button:
%a.btn.btn-primary.create_new{:href => admin_export_stories_path(:release => @release, format: "xls")}
   %i.fa.fa-plus
   Export


Step 3:


Create a action in controller


app/controller/admin/stories_controller.rb


def export_stories_report
if params[:release].present?
@release = Release.find(params[:release])
@stories = @release.stories
respond_to do |format|
   format.xls{send_data @stories.to_xls(:only => [:id, :project_id, :release_id, :release_name, :title, :if, :when, :then, :estimated_hours, :tags, :status]) }
end
end
end


Step 4:


Add Mime type
config/initializer/mime_type.rb

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


No comments:

Post a Comment