Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gem axlsx how to set font font_name?

Tags:

ruby

axlsx

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.

like image 401
greenif Avatar asked Apr 09 '13 12:04

greenif


1 Answers

Looking at your style declaration, it seems appropriate to me. At the risk of sounding pedantic, you should capitalized the font name.

Combining your bits and the nice example from acsmith, the following code should work fine for you in excel. What software are you using to view the Axlsx file? Not all spreadsheet software fully/implements the OOXML spec.

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")

best

randym

like image 149
randym Avatar answered Oct 14 '22 06:10

randym