Nic*_*ley 5 ruby excel ruby-on-rails ruby-on-rails-3 axlsx
我正在尝试格式化报表,并确保列的宽度正确,而auto_width似乎无法实现。
有了这段代码,这就是我得到的那种报告
。请注意,该空间将需要一个auto_width,因为如果我双击Excel中的每个列边框,它将正确调整大小,请参见此图
。
也许是我做事的顺序?
这是我正在使用的代码:
workbook = xlsx_package.workbook
worksheet_name = 'My Worksheet'
xlsx_package.use_autowidth = true
# Styles init
header_style = workbook.styles.add_style :bg_color => "D8D8D8",
:b => true,
:border => { :style => :thin, :color => "00" },
:alignment => { :horizontal => :center,
:vertical => :center ,
:wrap_text => false}
totals_style = workbook.styles.add_style :bg_color => "D8D8D8",
:b => true,
:border => { :style => :thin, :color => "00" },
:alignment => { :horizontal => :center,
:vertical => :center ,
:wrap_text => false}
odd_row_style = workbook.styles.add_style :bg_color => "A9E2F3",
:border => { :style => :thin, :color => "00" },
:alignment => { :horizontal => :center,
:vertical => :center ,
:wrap_text => false}
even_row_style = workbook.styles.add_style :bg_color => "CEECF5",
:border => { :style => :thin, :color => "00" },
:alignment => { :horizontal => :center,
:vertical => :center ,
:wrap_text => false}
merged_title_cell_style = workbook.styles.add_style :bg_color => "D8D8D8",
:b => true,
:sz => 16,
:border => { :style => :thin, :color => "00" },
:alignment => { :horizontal => :center,
:vertical => :center ,
:wrap_text => true}
workbook.add_worksheet(:name => worksheet_name) do |sheet|
# Add empty row for aesthetics
sheet.add_row([''], :height => 8)
# We add the meta header row
meta_header_row = ['', "Meta header 1", '', '', '', '', '', '', '', '', '', "Meta header 2", '', '', '', '']
sheet.add_row(meta_header_row, :style => merged_title_cell_style, :height => 30)
sheet.merge_cells('B2:K2')
sheet.merge_cells('L2:P2')
@data = Array.new
@data << ['John', 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, 11, 12, 13, 14, 15]
@data << ['Jack', 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, 11, 12, 13, 14, 15]
@data << ['Bob', 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, 11, 12, 13, 14, 15]
@data << ['Franck', 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, 11, 12, 13, 14, 15]
@data << ['A total', 4, 8, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64]
@data << ['Another total', 4, 8, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64]
@data.each_with_index do |data_row, index|
if(index == 0)
sheet.add_row(data_row, :style => header_style)
elsif(index >= @data.count - 2)
sheet.add_row(data_row, :style => totals_style)
elsif(index.even?)
sheet.add_row(data_row, :style => even_row_style)
else
sheet.add_row(data_row, :style => odd_row_style)
end
end
# Styling
sheet.col_style(0, header_style)
# We keep the first 2 cells white
sheet.rows[0..1].each{|row| row.cells[0].style = 0}
sheet.auto_filter = "A3:A#{sheet.rows.count}"
end
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
我也遇到过这个问题。我假设您正在寻找尽可能模仿收缩包装的“我只是双击每个列标题”外观的解决方案。不幸的是,我发现这很难做到。
可能对您有帮助的是(未记录的?)宽度类型:
sheet.add_row(meta_header_row,
:style => merged_title_cell_style,
:height => 30,
:widths => [:ignore] * meta_header_row.count # caution: Use *:widths* not :width
)
Run Code Online (Sandbox Code Playgroud)
这告诉 Axlsx 您仍然希望使用 autowidth 来计算列宽,但在这样做时要忽略该行中任何单元格的内容。
| 归档时间: |
|
| 查看次数: |
6557 次 |
| 最近记录: |