Ant Design Pro和Vue实现导出Excel

第一步,下载 js-export-excel

npm i  js-export-excel

第二步引入

import ExportJsonExcel from "js-export-excel"

第三步使用

 const exportExcel = () => {
        const datas = dataList ? dataList : '';//导出的数据
        const option = {};//定义一个容器,存储Excel文件信息
        const dataTable = [];//定义一个容器,存储Excel表格每行数据
        if (datas) {
            for (const i in datas) {  //循环数据
            	//obj存储的是行的信息
                const obj = {
                    'ID': datas[i].id,
                    '姓名': datas[i].name,
                    '性别': datas[i].sex,
                    '手机号': datas[i].phone,
                    '部门': datas[i].branch.map((item: { name: any; }) => item.name).join(','),
                    '邮箱': datas[i].email
                }
                dataTable.push(obj)
            }
        }
        // 文件名称
        option.fileName = '公司员工表'
        option.datas = [
            {
                sheetData: dataTable,
                sheetName: 'sheet',
                sheetFilter: ['ID', '姓名', '性别', '手机号', '部门', '邮箱'],
                sheetHeader: ['ID', '姓名', '性别', '手机号', '部门', '邮箱'],
            }
        ];
		//生成Excel文件对象
        const toExcel = new ExportJsonExcel(option);
        //下载到本地
        toExcel.saveExcel();
    }

猜你喜欢

转载自blog.csdn.net/weixin_65565362/article/details/127198874
今日推荐