Class: Labimotion::XlsxExporter
- Inherits:
-
Object
- Object
- Labimotion::XlsxExporter
- Defined in:
- lib/labimotion/libs/xlsx_exporter.rb
Overview
XlsxExporter A common utility class for exporting data to Excel (.xlsx) format using the caxlsx gem
Usage Example:
exporter = Labimotion::XlsxExporter.new('MyReport')
exporter.add_worksheet('Sheet1') do |sheet|
sheet.add_header(['Name', 'Age', 'Email'])
sheet.add_row(['Jane Smith', 25, 'jane@example.com'])
end
exporter.save_to_file('output.xlsx')
# or get the stream: exporter.to_stream
Defined Under Namespace
Classes: SheetBuilder, StyleManager
Instance Attribute Summary collapse
-
#package ⇒ Object
readonly
Returns the value of attribute package.
-
#workbook ⇒ Object
readonly
Returns the value of attribute workbook.
Instance Method Summary collapse
-
#add_worksheet(sheet_name, _options = {}) {|sheet| ... } ⇒ SheetBuilder
Add a new worksheet to the workbook.
-
#filename ⇒ String
Get suggested filename with .xlsx extension.
-
#initialize(filename = nil) ⇒ XlsxExporter
constructor
Initialize a new XlsxExporter.
-
#read ⇒ String
Read the Excel file content.
-
#save_to_file(filepath) ⇒ Boolean
Save the Excel file to disk.
-
#to_stream ⇒ String
Get the Excel file as a stream.
Constructor Details
#initialize(filename = nil) ⇒ XlsxExporter
Initialize a new XlsxExporter
22 23 24 25 26 27 28 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 22 def initialize(filename = nil) @filename = filename @package = Axlsx::Package.new @workbook = @package.workbook @workbook.styles.fonts.first.name = 'Calibri' @current_sheet = nil end |
Instance Attribute Details
#package ⇒ Object (readonly)
Returns the value of attribute package.
18 19 20 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 18 def package @package end |
#workbook ⇒ Object (readonly)
Returns the value of attribute workbook.
18 19 20 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 18 def workbook @workbook end |
Instance Method Details
#add_worksheet(sheet_name, _options = {}) {|sheet| ... } ⇒ SheetBuilder
Add a new worksheet to the workbook
35 36 37 38 39 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 35 def add_worksheet(sheet_name, = {}, &block) sheet = SheetBuilder.new(@workbook.add_worksheet(name: sheet_name), @workbook) sheet.instance_eval(&block) if block_given? sheet end |
#filename ⇒ String
Get suggested filename with .xlsx extension
66 67 68 69 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 66 def filename name = @filename || "export_#{Time.now.strftime('%Y%m%d_%H%M%S')}" "#{name}.xlsx" end |
#read ⇒ String
Read the Excel file content
49 50 51 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 49 def read @package.to_stream.read end |
#save_to_file(filepath) ⇒ Boolean
Save the Excel file to disk
56 57 58 59 60 61 62 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 56 def save_to_file(filepath) @package.serialize(filepath) true rescue StandardError => e Labimotion.log_exception(e) false end |
#to_stream ⇒ String
Get the Excel file as a stream
43 44 45 |
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 43 def to_stream @package.to_stream end |