sheetjs xlsx,如何编写合并的单元格?

时间:2018-11-28 09:42:14

标签: javascript xlsx sheetjs

我需要使用sheetjs创建具有合并单元格的xlsx。

数据

[
  {
    "id": "nick",
    "items": [
      {
        "name": "ball"
      },
      {
        "name": "phone"
      }
    ]
  },
  {
    "id": "jack",
    "items": [
      {
        "name": "pen"
      },
      {
        "name": "doll"
      }
    ]
  }
]

我的代码:

var ws = XLSX.utils.json_to_sheet(data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "");
var wbout = XLSX.write(wb, {bookType:'xlsx', type:'array'});
saveAs(new Blob([wbout],{type:"application/octet-stream"}), filename + ".xlsx");

我想要获得的结果:

result

如何获得此结果?

...谢谢

1 个答案:

答案 0 :(得分:0)

const merge = [
  { s: { r: 1, c: 0 }, e: { r: 2, c: 0 } },{ s: { r: 3, c: 0 }, e: { r: 4, c: 0 } },
];
ws["!merges"] = merge;

将此代码用于合并A2:A3({ s: { r: 1, c: 0 }, e: { r: 2, c: 0 } }) 和A4:A5({ s: { r: 3, c: 0 }, e: { r: 4, c: 0 } }

这里s =开头,r =行,c = col,e =结束