纯前端xlsx实现excel导出

// 将table的表单的tableData数据转化为二位数组
const bodyData = tableData.map(x=>[x.key...])

//定义简单的一级表头
//将columus的label作为表头

const header = []
header.push(columns.map((x) => x.label))

body.unshift(...header);// 将表头添加到 body 中
const workbook = xlsx.utils.book_new();// 创建虚拟的 workbook
const sheet = xlsx.utils.aoa_to_sheet(bodyData);// aoa_to_sheet 将二维数组转成 sheet


// 设置行高和列宽
 const cols = [
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
      { wch: 10 },
    ]
 sheet['!cols'] = cols // 将cols添加到sheet中,设置列宽
 const rows = [{ hpx: 20 }, { hpx: 16 }, { hpx: 18 }]
 sheet['!rows'] = rows // 将rows添加到sheet中,设置行高
 xlsx.utils.book_append_sheet(workbook, sheet, 'sheet名称') // 向 workbook 中添加 sheet
    xlsx.writeFile(
      workbook,
      `文档名称.xlsx`
    )

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值