Last active
August 29, 2015 14:07
-
-
Save rebelweb/d2404392f13ead1ead28 to your computer and use it in GitHub Desktop.
Axlsx Report Object Oriented Style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '[email protected]']} | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Reports | |
class Report | |
#places methods and pieces used by multiple reports here | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.