From e6d269d03875bc0a4687b9d5f4b7016fc7d27b32 Mon Sep 17 00:00:00 2001 From: Randy Morgan Date: Sun, 27 Nov 2011 21:44:02 +0900 Subject: updating the readme to give a better example of what can be done with rails xlsx --- README.md | 55 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8fc42120..d0d834f2 100644 --- a/README.md +++ b/README.md @@ -196,30 +196,51 @@ Styling Rows Rails 3 #ImageMagick port needs to be confirgured --disable-openmp when using with rails3 - # http://stackoverflow.com/questions/2838307/why-is-this-rmagick-call-generating-a-segmentation-fault + #http://stackoverflow.com/questions/2838307/why-is-this-rmagick-call-generating-a-segmentation-fault - # 1. Add the gem to your Gemfile and bundle install + #Add the gem to your Gemfile and bundle install + gem 'axlsx' - gem 'axlsx' - - # 2. Add 'acts_as_axslx' to your model - - class MyModel < ActiveRecord::Base - acts_as_axlsx - end + # A Model that has id, name, title, content, vote, popularity, created_at and updated_at attributes + class Post < ActiveRecord::Base - # 3. In your controlle, simply call to_xlsx against the class - - class MyModelController < ApplicationController + # support for :include, :exclude, :only like ruport has not been implemented yet + acts_as_axlsx - #GET /posts/xlsx - def xlsx - p = MyModel.since(Time.now - 5.days).to_xlsx - p.serialize('my_model.xlsx') - send_file 'my_model.xlsx', :type=>"application/xlsx", :x_sendfile=>true + def report + # First generate your package from the model + # scopes, conditions etc are allowed but must be before the to_xlsx call. + p = Post.to_xlsx + + # Add in some basic styles and formats + percent, date, header = nil, nil, nil + p.workbook.styles do |s| + percent = s.add_style :num_fmt=>9 + date = s.add_style :format_code => 'yyyy/mm/dd' + header = s.add_style :bg_color => "FF000000", :fg_color => "FFFFFFFF", :alignment => {:horizontal=>:center}, :sz=>14 + end + + ws = p.workbook.worksheets.first + + # Update the row and columns style so they propery format data. + ws.row_style(0, header) + ws.col_style(5, percent, :row_offset=>1) + ws.col_style((6..7), date, :row_offset=>1) + + # Add a chart for good measure + ws.add_chart(Axlsx::Bar3DChart) do |chart| + chart.title='Post Votes' + chart.start_at 0, ws.rows.size + chart.end_at 4, ws.rows.size+20 + chart.add_series :data => ws.cols[4][(1..-1)], :labels=>ws.cols[0][(1..-1)] + end + + p.serialize('posts.xlsx') + send_file 'posts.xlsx', :type=>"application/xlsx", :x_sendfile=>true end end + ###Documentation This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use: -- cgit v1.2.3