Node.js @zurmokeeper/exceljs 如何快速导出多表头的excel文件

Node.js 如何快速导出嵌套列(多表头)的excel文件。效果图如下:

image.png

1:使用 @zurmokeeper/exceljs, V4.4.1以上 安装:

npm i @zurmokeeper/exceljs

2: 有一个 worksheet.makeColumns 方法,API文档: 代码示例:

const ExcelJS = require('@zurmokeeper/exceljs');
(async ()=>{
  const test = 'test.xlsx';

    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('Sheet1', {
      views: [{state: 'frozen', xSplit: 0, ySplit: 2}], // 冻结第1行和第二行
    });
	// 设置多级表头
    worksheet.makeColumns([
      {
        id: 1,
        title: '姓名',
      },
      {id: 2, title: 'Qwe'},
      {id: 3, title: 'Foo'},
      {
        id: 4,
        title: '基础信息',
        children: [
          {id: 41, title: 'Zoo 1'},
          {id: 42, title: 'Zoo 2'},
          {id: 44, title: 'Zoo 3'},
        ],
      },
      {
        id: 5,
        title: 'Zoo1',
        children: [
          {id: 51, title: 'Zoo 51'},
          {id: 52, title: 'Zoo 52'},
          {id: 54, title: 'Zoo 53'},
        ],
      },
      {id: 6, title: 'Foo123213'},
    ]);
    const data = [
      [1, 2, 3, null, null, 'DOB'],
      [1, 'electron', 'DOB'],
      [1, 'electron', 'DOB'],
      [1, 'electron', 'DOB'],
      [1, 'electron', 'DOB'],
      [1, 'electron', 'DOB'],
      [1, 'electron', 'DOB'],
      [1, 'electron', 'DOB'],
    ];
    worksheet.addRows(data);
    worksheet.columns.forEach(function(column) {
      column.alignment = {horizontal: 'center', vertical: 'middle'};
    });
    await workbook.xlsx.writeFile(test);

})()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值