如何设置工作簿的列宽

时间:2017-01-10 12:00:53

标签: ruby-on-rails ruby excel axlsx

目前我正在做这样的事情

wb = xlsx_package.workbook

wb.styles do |s|
head_cell = s.add_style :b => true, :alignment => { :horizontal=> :center }
empty_cell = s.add_style :bg_color => "808080"

wb.add_worksheet(name: "Export") do |sheet|
sheet.add_row ['Modulverantwortliche','Dozenten', 'Modultitel', 'CP', 'SWS', 'Sommer/Winter'], :style => [head_cell, head_cell, empty_cell, head_cell, head_cell, head_cell]

foo = getUserDetails()

foo.each do |l|
        modul = l[:object]
        turnus = ""
        if modul.semester_id == 1
        turnus = "s"
        elsif modul.semester_id == 2
        turnus = "w"
        else
        turnus = "s/w"
        end
        sheet.add_row [ modul.modultitel, modul.modulbeauftragte, modul.lehrende, modul.creditpoint, modul.sws, turnus]  
    end
end

现在我需要为第一列和第二列设置宽度。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

使用 column_widths 功能。

Here你可以看到函数

here你可以看到文档

代码有点像

...
wb.add_worksheet(name: "Export") do |sheet|
    ...
    sheet.add_row [ ...... ]
    sheet.add_row [ ...... ]
    ...
    sheet.column_widths 7.2, nil, 3
end
...

编辑:@asdlfkjlkj的好点:你必须在添加数据后调用column_widths,否则宽度将无法成功设置