【发布时间】:2014-11-14 15:18:46
【问题描述】:
我正在使用 AXLSX Ruby gem 生成 Excel 报告,但在以模块化方式将多种样式应用于单元格时遇到问题。
这是一个只有两种样式(“背景”和“背景粗体”)的示例,
require 'axlsx'
axlsx = Axlsx::Package.new
workbook = axlsx.workbook
with_background = workbook.styles.add_style bg_color: "E2D3EB"
bold_with_background = workbook.styles.add_style bg_color: "E2D3EB", b: true
workbook.add_worksheet do |sheet|
sheet.add_row
sheet.add_row ["", "Product", "Category", "Price"], style: [0, bold_with_background, bold_with_background, bold_with_background]
sheet.add_row ["", "Butter", "Dairy", 4.99], style: [0, with_background, with_background, with_background]
sheet.add_row ["", "Bread", "Baked Goods", 3.45], style: [0, with_background, with_background, with_background]
sheet.add_row ["", "Broccoli", "Produce", 2.99], style: [0, with_background, with_background, with_background]
end
axlsx.serialize "grocery.xlsx"
结果如下,
现在,假设我必须在此表周围应用边框。如果我理解正确的话,我必须有很多样式才能到达那里:“左上边缘在背景上加粗”,“上边缘在背景上加粗”,“右上边缘在背景上加粗”,“在右边缘的背景上”等。
有没有一种方法可以将多个样式应用于单元格,而不必为每个可能的基本样式组合声明一个样式?
我想要类似的东西
sheet["B2"].add_style(bold).add_style(background).add_style(top_left_edge)
但不确定 gem 是否实现了类似的解决方案。
有什么想法吗?谢谢!
【问题讨论】: