原生table表格使用js导出excel表

1、安装相关依赖
主要是两个依赖

npm install --save xlsx file-saver

2、组件里头引入

	 import FileSaver from 'file-saver'
	 import * as XLSX from 'xlsx'

3、组件methods里写一个方法

// 表格导出
			exportExcel(name) {
				/* generate workbook object from table */
				var wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
				/* get binary string as output */
				var wbout = XLSX.write(wb, {
					bookType: 'xlsx',
					bookSST: true,
					type: 'array'
				})
				try {
					FileSaver.saveAs(new Blob([wbout], {
						type: 'application/octet-stream'
					}), name+'.xlsx')
				} catch (e) {
					if (typeof console !== 'undefined') console.log(e, wbout)
				}
				return wbout
			},

注:#out-table是表格dom的id
     name  是要设置的表单名称

4、使用

<button style="cursor:pointer" @click="exportExcel('省内客流来源统计报表')">点击导出</button>

猜你喜欢

转载自blog.csdn.net/weixin_46408500/article/details/127865069