Skip to content

Instantly share code, notes, and snippets.

@rebelweb
Last active August 29, 2015 14:07
Show Gist options
  • Save rebelweb/d2404392f13ead1ead28 to your computer and use it in GitHub Desktop.
Save rebelweb/d2404392f13ead1ead28 to your computer and use it in GitHub Desktop.
Axlsx Report Object Oriented Style
require 'axlsx'
module Reports
class AxlsxReport < Reports::Report
##call from irb (or anything ruby for that matter) to generate report (i.e. Reports::AxlsxReport.generate)
def self.generate
Alsx::Package.new do |p|
p.workbook.add_worksheet(:name => 'Users') {|ws| users_worksheet(ws)}
p.workbook.add_worksheet(:name => 'User Groups') {|ws| user_groups_worksheet(ws)}
p.serialize 'axlsx_report.xlsx'
end
end
private
#place all builder methods here
def self.users_worksheet(ws)
ws.add_row users_title_row
100.times {ws.add_row ['#', 'John Doe', 'jdoe', 'abc@domain.com']}
end
def self.user_groups_worksheet(ws)
ws.add_row profiles_title_row
10.times {ws.add_row ['#', 'General User', 'GU'])
end
def self.users_title_row
['id', 'name', 'username', 'email']
end
def self.profiles_title_row
['#', 'Description', 'Code']
end
end
end
module Reports
class Report
#places methods and pieces used by multiple reports here
end
end
@rebelweb
Copy link
Author

rebelweb commented Oct 2, 2014

This is basic ALSX Report, built using a object oriented design. This a very basic design and layout for simplicity, but this style can also be used with Rails or a straight database connection using gems like pg, mysql, etc. to generate dynamic reports. It shows it inherits from a master Report class where you can share pieces and parts among multiple reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment