我一直在使用Spreadsheet gem来编写xls文件.使用Spreadsheet gem,可以根据行和列号动态完成单元格的合并
merge_cells(start_row, start_col, end_row, end_col)
Run Code Online (Sandbox Code Playgroud)
我的代码片段
excel_file = Spreadsheet::Workbook.new
sheet1 = excel_file.create_worksheet :name => 'Example'
.
.#code blocks
.
.
start_col = 4
end_col = 0
wk_array_size.each_with_index do |v,i|
end_col = start_col+((v.to_i*2)-1)
sheet1.merge_cells(0, start_col, 0, end_col)
start_col = (end_col+3)
end
.
.#code blocks
.
sheet1.insert_row(0,week_names)
Run Code Online (Sandbox Code Playgroud)
其中week_array_size是一个数组,其中包含基于其合并单元格的散列的大小.
[11, 10, 3]
Run Code Online (Sandbox Code Playgroud)
合并将在数组week_array_size上迭代地动态完成
start_col = 4
end_col = (4+(11*2)-1) = 25
(0, 4, 0, 25)
sheet1.merge_cells(0, 4, 0, 25)
.
.
(0, 28, 0, 47)
sheet1.merge_cells(0, 28, 0, 47) …Run Code Online (Sandbox Code Playgroud)