如何在 Rails 中使用 Axlsx Gem 创建页眉和页脚?

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

我正在尝试使用 rails 中的以下代码创建页眉和页脚。但它不会在 Excel 文件中创建页眉和页脚。我哪里做错了?

     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

我建议您添加一行并将样式属性应用到该行。 在下面的代码中,我已将背景颜色更改为灰色,文本为 header 加粗。希望这对您有所帮助!!

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

来自文档 here

header_footer = {:different_first => false, :odd_header => '&L&F : &A&R&D &T', :odd_footer => '&C&Pof&N'}
wb.add_worksheet(:name => "header footer", :header_footer => header_footer) do |sheet|
  sheet.add_row ["this sheet has a header and a footer"]
end