Vue2使用el-upload做excel上传导入功能

本文介绍了如何在Vue项目中使用npm或yarn安装xlsx库,实现文件上传功能,并解析.xlsx和.xls类型的Excel文件,包括使用el-upload组件、异步处理上传事件和解析工作簿内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、安装插件

npm install xlsx
或
yarn add xlsx

2、页面引入使用

<el-upload :auto-upload="false" :before-remove="beforeRemove" :file-list="fileList" :on-change="handleChange" :on-remove="handleRemove" :show-file-list="true" accept=".xlsx, .xls" action class="upload-box">
	<el-button size="small" type="primary">点击上传</el-button>
	<div class="el-upload__tip" slot="tip" v-show="!fileList[0]">未选择任何文件</div>
	<div slot="tip" class="el-upload__tip">仅支持.xlsx, .xls类型文件</div>
</el-upload>
<script>
	import * as XLSX from 'xlsx/xlsx.mjs'
	export default {
		data() {
			return {
				lastUid: null,
				fileList: [],
			}
		},
		methods: {
			async handleChange(ev, fileList) {
				if (fileList.length > 0) {
					this.fileList = [fileList[fileList.length - 1]]
				} else {
					this.fileList = fileList[0]
				}
				let file = ev.raw
				if (this.lastUid == file.uid) {
					return
				}
				this.lastUid = file.uid
				if (file) {
					console.log(file)
					if (
						file.name.indexOf('.xls') != -1 ||
						file.name.indexOf('.xlsx') != -1
					) {
						let data = await this.readFile(file)
						let workbook = XLSX.read(data, {
							type: 'binary',
							cellDates: true
						})
						let worksheet = workbook.Sheets[workbook.SheetNames[0]]
						let secondsheet = workbook.Sheets[workbook.SheetNames[1]]
						let resultExcel = XLSX.utils.sheet_to_json(worksheet)
						let tableHead = Object.keys(resultExcel[0]);
						console.log(resultExcel, '解析后表格数据')
				        console.log(tableHead, '解析后表格头')
					}
				}
			},
			readFile(file) {
				return new Promise(resolve => {
					let reader = new FileReader()
					reader.readAsBinaryString(file)
					reader.onload = ev => {
						resolve(ev.target.result)
					}
				})
			},
			handleRemove(file, fileList) {
				this.fileList = []
				this.resultExcel = []
			},
			beforeRemove(file, fileList) {
				return this.$confirm(`确定移除 ${file.name}?`)
			},
		}
	}
</script>

3、封装全局组件globalUpload.vue

在components文件夹下新建globalUpload.vue
<template>
	<div>
		<el-upload :auto-upload="false" :before-remove="beforeRemove" :file-list="fileList" :on-change="handleChange"
			:on-remove="handleRemove" :show-file-list="true" accept=".xlsx, .xls" action class="upload-box">
			<el-button size="small" type="primary">点击上传</el-button>
			<div class="el-upload__tip" slot="tip" v-show="!fileList[0]">未选择任何文件</div>
			<div slot="tip" class="el-upload__tip">仅支持.xlsx, .xls类型文件</div>
		</el-upload>
	</div>
</template>

<script>
	import * as XLSX from 'xlsx/xlsx.mjs'
	export default {
		data() {
			return {
				lastUid: null,
				fileList: [],
			}
		},
		methods: {
			async handleChange(ev, fileList) {
				if (fileList.length > 0) {
					this.fileList = [fileList[fileList.length - 1]]
				} else {
					this.fileList = fileList[0]
				}
				let file = ev.raw
				if (this.lastUid == file.uid) {
					return
				}
				this.lastUid = file.uid
				if (file) {
					console.log(file)
					if (
						file.name.indexOf('.xls') != -1 ||
						file.name.indexOf('.xlsx') != -1
					) {
						let data = await this.readFile(file)
						let workbook = XLSX.read(data, {
							type: 'binary',
							cellDates: true
						})
						let worksheet = workbook.Sheets[workbook.SheetNames[0]]
						let secondsheet = workbook.Sheets[workbook.SheetNames[1]]
						let resultExcel = XLSX.utils.sheet_to_json(worksheet)
						let tableHead = Object.keys(resultExcel[0]);
						this.$emit('handleChange',{
							resultExcel,
							tableHead
						})
					}
				}
			},
			readFile(file) {
				return new Promise(resolve => {
					let reader = new FileReader()
					reader.readAsBinaryString(file)
					reader.onload = ev => {
						resolve(ev.target.result)
					}
				})
			},
			handleRemove(file, fileList) {
				this.fileList = []
				this.resultExcel = []
			},
			beforeRemove(file, fileList) {
				return this.$confirm(`确定移除 ${file.name}?`)
			},
		}
	}
</script>

<style lang="scss" scoped></style>

4、全局注册:main.js文件引入注册

import globalUpload from './components/globalUpload.vue';
Vue.component('global-upload', globalUpload);

5、单页面使用:

<template>
	<div>
		<global-upload @handleChange='handleChange'></global-upload>
		<el-table :data="resultExcel" style="width: 100%" v-if="resultExcel.length > 0" border max-height="600">
			<el-table-column v-for="column in tableHead" :key="column" :prop="column" :label="column" show-overflow-tooltip>
			</el-table-column>
		</el-table>
	</div>
</template>

<script>
	export default {
		name: 'sailSoft',
		data() {
			return {
				resultExcel: [], //解析后表格数据
				tableHead: [], //动态表头数据
			}
		},
		methods: {
			handleChange(e){
				this.resultExcel = e.resultExcel
				this.tableHead = e.tableHead
				console.log(this.resultExcel, '解析后表格数据')
				console.log(this.tableHead, '解析后表格头')
			}
		}
	}
</script>

<style lang="scss" scoped></style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值