安装xlsx
npm install --save xlsx
const exportExcel = () => {
const wbout = XLSX.write(
XLSX.utils.table_to_book(document.getElementById('table'), { raw: true }),
{
bookType: 'xlsx',
bookSST: true,
type: 'array'
}
);
const blob = new Blob([wbout], { type: 'application/octet-stream' });
const fileName = `${classCourseName}.xlsx`; // 导出的excel名字
if ('download' in document.createElement('a')) {
const elink = document.createElement('a');
elink.download = fileName;
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
} else {
navigator.msSaveBlob(blob, fileName);
}
};
<Button type="primary" ghost className={styles.btn} onClick={exportExcel}>
导出Excel
</Button>
<Table id="table"></Table>