2

我正在使用带有导轨的 Axlsx gem 来创建 Excel 工作表。我需要修复和冻结标题。即使我们向下滚动,标题也应该始终可见。导轨版本 3.2.1 宝石'axlsx'

任何帮助表示赞赏?

4

2 回答 2

8

您可以像以下示例一样冻结窗格:

  require 'axlsx'
  XLSX_temp = 'simple.xlsx'

  Axlsx::Package.new do |p|
      p.workbook.add_worksheet(:name => 'DATA') do |sheet|
        sheet.add_row(%w{key col1 col2 col3 col4 col5})
        #Fix first line and column
        sheet.sheet_view.pane do |pane|
          pane.top_left_cell = "B2"
          pane.state = :frozen_split
          pane.y_split = 1
          pane.x_split = 1
          pane.active_pane = :bottom_right
        end

        10.times{
          sheet.add_row(%w{1 2 3 4 5 6})
        }
      end    

      puts "Write %s" % XLSX_temp
      p.serialize(XLSX_temp)
  end

结果具有固定的第一列和第一行:

在此处输入图像描述

pane.x_split = 0只有第一行是固定的。

于 2016-02-24T10:51:43.633 回答
1

在此处尝试 axlsx 示例页面:

https://github.com/randym/axlsx/blob/master/examples/example.rb#L575-L581

# Repeat printing of header rows.
#```ruby
if examples.include? :repeated_header
  wb.add_worksheet(:name => "repeated header") do |sheet|
    sheet.add_row %w(These Column Header Will Render On Every Printed Sheet)
    200.times { sheet.add_row %w(1 2 3 4 5 6 7 8) }
    wb.add_defined_name("'repeated header'!$1:$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Titles')
  end
end
于 2015-08-22T04:47:45.103 回答