7

I use axlsx gem to work with xlsx file. Please help me to set font in cells of sheet.

item_style = s.add_style :b => false, :sz => 9,  :font_name => 'courier',
      :alignment => { :horizontal => :left, :vertical => :center, :wrap_text => true}
row = sheet.add_row [item.name, item.price], :style => item_style

But font in cells still 'Arial'. I need any 'mono width' font. I know that 'courier' is not mono width font, use it just for example.

Because I have fixed column width. And I want to know when text in cell takes 2 lines. To set appropriate row height.

Thanks.

4

3 回答 3

7

看你的风格声明,我觉得很合适。冒着听起来迂腐的风险,您应该将字体名称大写。

结合你的位和来自 acsmith 的好例子,下面的代码在 excel 中应该可以正常工作。您使用什么软件查看 Axlsx 文件?并非所有电子表格软件都完全/实现了 OOXML 规范。

require 'axlsx'
p = Axlsx::Package.new
wb = p.workbook
item_style = wb.styles.add_style :b => false, :sz => 9,  :font_name => 'Courier',
  :alignment => { :horizontal => :left, :vertical => :center, :wrap_text => true}
wb.add_worksheet(:title => "Worksheet 1") do |sheet|
  sheet.add_row(["text in Courier"], :style => item_style)
end
p.serialize("courier.xlsx")

最好的

随机的

于 2013-04-10T11:06:29.747 回答
2

我建议尝试以下迷你示例并确保它有效。您需要将整个内容放在样式块中。

p = Axlsx::Package.new
  wb = p.workbook
  wb.styles do |s|
    courier = s.add_style :font_name => "Courier"
    wb.add_worksheet(:title => "Worksheet 1") do |sheet|
      sheet.add_row(["text in Courier"], :style => courier)
    end
  end
p.serialize("Courier.xlsx")

我没有太多使用 axlsx,但我相信任何使用的样式都必须在样式块中声明,并在该块中使用。

于 2013-04-09T18:30:48.490 回答
2

由我们 Axlsx gem ,font_name可以像这样多种方式设置,这对我有用:

sheet.add_row ["some data", "","","", "", "","",""], :sz => 9,:height => 16,:font_name => "Arial"   -------------> first way

sheet.rows.last.cells[0].font_name = "Arial"  ----------> second way

sheet["A10"].font_name = "Arial"  -----------> third way

对于多行:

sheet["A1:E1"].each { |c| c.font_name = "Arial" } -------> fourth way

@arial_fontfamily = s.add_style :b => 'true', :sz => 10,  :font_name =>   'Arial'  --> small css definition  -------> fifth way

sheet["A1:E1"].each { |c| c.style = @arial_fontfamily  } -------> sixth way

sheet.add_row ["some data", "","","", "", "","",""], :sz => 9,:height => 16,:style => @arial_fontfamily   -------------> seventh way
于 2015-12-03T12:05:13.257 回答