Class: Labimotion::XlsxExporter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ XlsxExporter

Initialize a new XlsxExporter

Parameters:

  • filename (String) (defaults to: nil)

    optional filename (without extension)



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

#packageObject (readonly)

Returns the value of attribute package.



18
19
20
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 18

def package
  @package
end

#workbookObject (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

Parameters:

  • sheet_name (String)

    name of the worksheet

  • options (Hash)

    additional options for the worksheet

Yields:

  • (sheet)

    gives the SheetBuilder to the block

Returns:



35
36
37
38
39
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 35

def add_worksheet(sheet_name, _options = {}, &block)
  sheet = SheetBuilder.new(@workbook.add_worksheet(name: sheet_name), @workbook)
  sheet.instance_eval(&block) if block_given?
  sheet
end

#filenameString

Get suggested filename with .xlsx extension

Returns:

  • (String)

    filename with 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

#readString

Read the Excel file content

Returns:

  • (String)

    the Excel file binary 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

Parameters:

  • filepath (String)

    path where to save the file

Returns:

  • (Boolean)

    true if saved successfully



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_streamString

Get the Excel file as a stream

Returns:

  • (String)

    the Excel file stream



43
44
45
# File 'lib/labimotion/libs/xlsx_exporter.rb', line 43

def to_stream
  @package.to_stream
end