小编Yug*_*ran的帖子

Axlsx基于行列号合并单元格,如Spreadsheet gem

我一直在使用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)

ruby excel ruby-on-rails spreadsheet axlsx

5
推荐指数
1
解决办法
4316
查看次数

标签 统计

axlsx ×1

excel ×1

ruby ×1

ruby-on-rails ×1

spreadsheet ×1