Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-4rubygemsaxlsx

How to create Header and footer using Axlsx Gem in Rails?


I am trying to create the header and footer with the below code in the rails. But it is not creating the header and footer in the Excel file. Where I did wrong?

     xlsx_package = Axlsx::Package.new
        wb = xlsx_package.workbook
        header_footer = {:different_first => false, odd_header:'&C&14 &B&UHeader Text', odd_footer:'&L&11&BPage &P &C&11Footer Text &R&11&ILast updated:&D'}
        wb.add_worksheet(name: "header footer", :header_footer => header_footer) do |sheet|
             sheet.add_row ["First Column", "Second", "Third"]
             sheet.add_row [1, 2, 3]
       end

Solution

  • I suggest you to add a row & apply style properties to this row. In below code, I have changed the background color to gray & text is made bold for header. Hope this helps!!

    p = Axlsx::Package.new
    wb = p.workbook
    
    head_style = wb.styles.add_style bg_color: "DDDDDD",  b: true
    
    wb.add_worksheet(name: "Sheet 1") do |sheet|
      sheet.add_row %w(Name Age)
      sheet.row_style 0, head_style
    
      sheet.add_row ['Tom',20]
      sheet.add_row ['Pete',22]
    
    end