The front end uses the exceljs plug-in to customize the table content to make excel and export it

// 安装插件
npm install exceljs
npm install file-saver

//代码部分 (也可封装成公共方法)
async excelExport() {
			const ExcelJS = require('exceljs')
			const fileSave = require('file-saver')
			const workbook = new ExcelJS.Workbook()
			const worksheet = workbook.addWorksheet('Sheet1')
			//根据数据自己调整
			// width: 24
			worksheet.columns = [
				{ header: '序号', key: 'name' },
				{ header: '项目id', key: 'projectId' },
				{ header: '姓名', key: 'name' },
				{ header: '金额', key: 'money' },
				{ header: '实际入账金额', key: 'realMoney', width: 15 },
				{ header: '状态说明', key: 'statusExplain', width: 20 },
				{ header: '备注', key: 'remark', width: 100 },
			]
            // 表格一些样式操作
			worksheet.columns.forEach((item, i) => {
                // monthSheet.getColumn(i + 1).width = 10
				worksheet.getColumn(i + 1).alignment = { vertical: 'middle', horizontal: 
                 'center' }
			})
			worksheet.getRow(1).font = {
				size: 12,
				bold: true,
			}
            // 塞入表格data数据 格式为数组,属性为对象,对象的key值对应columns的key值
			worksheet.addRows(this.excelData)
			workbook.xlsx.writeBuffer().then(buffer => {
				//这里为type
				const blob = new Blob([buffer], {
					type: 'application/vnd.openxmlformats- 
                     officedocument.spreadsheetml.sheet;charset=UTF-8',
				})
				fileSave(blob, `发放导入模板.xlsx`)
			})
		},

Guess you like

Origin blog.csdn.net/gcyaozuodashen/article/details/128325142