Module: Axlsx::Ar::SingletonMethods

Defined in:
lib/axlsx/ar.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) to_xlsx(number = :all, options = {})



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/axlsx/ar.rb', line 22

def to_xlsx(number = :all, options = {})
  row_style = options.delete(:style)
  header_style = options.delete(:header_style) || row_style
  types = options.delete(:types)

  data = [*find(number, options)]
  data.compact!
  data.flatten!
  columns = data.first.attributes.keys
  p = Package.new
  row_style = p.workbook.styles.add_style(row_style) unless row_style.nil?
  header_style = p.workbook.styles.add_style(header_style) unless header_style.nil?
  
  p.workbook.add_worksheet(:name=>table_name.humanize) do |sheet|
    sheet.add_row columns, :style=>header_style
    data.each do |r|
      sheet.add_row r.attributes.values, :style=>row_style, :types=>types
    end
  end
  p
end