// 将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`
)
纯前端xlsx实现excel导出
于 2023-12-26 11:44:16 首次发布