目前我正在做这样的事情
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
现在我需要为第一列和第二列设置宽度。我怎样才能做到这一点?
最佳答案
具有 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,否则将无法成功设置宽度
关于ruby-on-rails - 如何设置工作簿的列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41568374/