欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue上傳解析excel文件并在列表中輸出內(nèi)容

 更新時(shí)間:2023年09月14日 11:19:27   作者:還是大劍師蘭特  
在vue的項(xiàng)目開(kāi)發(fā)中,我們會(huì)遇到加載excel或者csv等情形,這個(gè)示例展示了這個(gè)需求,上傳一個(gè)excel文件,通過(guò)解析,生成數(shù)組,然后再列表中將內(nèi)容展示出來(lái),這篇文章主要介紹了vue上傳解析excel文件,列表中輸出內(nèi)容,需要的朋友可以參考下,

需求背景

在vue的項(xiàng)目開(kāi)發(fā)中,我們會(huì)遇到加載excel或者csv等情形,這個(gè)示例展示了這個(gè)需求。上傳一個(gè)excel文件,通過(guò)解析,生成數(shù)組,然后再列表中將內(nèi)容展示出來(lái)。

示例效果

在這里插入圖片描述

示例源代碼(共105行)

/*
* @Author: 大劍師蘭特(xiaozhuanlan),還是大劍師蘭特(CSDN)
* @此源代碼版權(quán)歸大劍師蘭特所有,可供學(xué)習(xí)或商業(yè)項(xiàng)目中借鑒,未經(jīng)授權(quán),不得重復(fù)地發(fā)表到博客、論壇,問(wèn)答,git等公共空間或網(wǎng)站中。
* @Email: 2909222303@qq.com
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2022-09-07
*/
<template>
	<div class="container">
		<div class="top">
			<h3>vue上傳解析excel文件,輸出列表內(nèi)容 </h3>
			<div class="author">大劍師蘭特, 還是大劍師蘭特,gis-dajianshi</div>
		</div>
		<h4>
			<el-upload ref="upload" action="/" :show-file-list="false" :on-change="importExcel" :auto-upload="false">
				<el-button slot="trigger" icon="el-icon-upload" size="small" type="success">上傳文件</el-button>
			</el-upload>
		</h4>
		<div style="width: 80%; margin:10px auto">
			<el-table :data="xlsxData" style="width: 100%">
				<el-table-column prop="name" label="姓名" width="180"></el-table-column>
				<el-table-column prop="class" label="班級(jí)" width="180"></el-table-column>
				<el-table-column prop="score" label="成績(jī)"></el-table-column>
				<el-table-column prop="level" label="等級(jí)"></el-table-column>
			</el-table>
		</div>
	</div>
</template>
<script>
	import * as XLSX from 'xlsx'
	export default {
		data() {
			return {
				wb: null,
				xlsxData: [],
			}
		},
		methods: {
			importExcel(file) {
				// let filefile = file.files[0] // 使用傳統(tǒng)的input方法需要加上這一步 
				const types = file.name.split('.')[1]
				const fileType = ['xlsx', 'xlc', 'xlm', 'xls', 'xlt', 'xlw', 'csv'].some(item => item === types)
				if (!fileType) {
					alert('格式錯(cuò)誤!請(qǐng)重新選擇')
					return
				}
				this.fileExchange(file).then(tabJson => {
			  if (tabJson && tabJson.length > 0) {
						this.xlsxData = tabJson[0].sheet
						console.log(this.xlsxData)
						// xlsxJson就是解析出來(lái)的json數(shù)據(jù),數(shù)據(jù)格式如下 
						// [ 
						//   { 
						//     sheetName: sheet1 
						//     sheet: sheetData 
						//   } 
						// ] 
					}
				})
			},
			fileExchange(file) {
				return new Promise(function(resolve, reject) {
					const reader = new FileReader()
			  reader.onload = function(e) {
						const data = e.target.result
						this.wb = XLSX.read(data, {
							type: 'binary'
						})
						const result = []
						this.wb.SheetNames.forEach((sheetName) => {
							result.push({
								sheetName: sheetName,
								sheet: XLSX.utils.sheet_to_json(this.wb.Sheets[sheetName])
							})
						})
						resolve(result)
					}
					reader.readAsBinaryString(file.raw)
			 	// reader.readAsBinaryString(file) // 傳統(tǒng)input方法 
				})
			}
		},
	}
</script>
<style scoped>
	.container {
		width: 1000px;
		height: 580px;
		margin: 50px auto;
		border: 1px solid green;
	}
	.top {
		margin: 0 auto 30px;
		padding: 10px 0;
		background: #23B486;
		color: #fff;
	}
</style>

安裝引用依賴(lài)

// 安裝插件 
npm install xlsx --save 
// 在vue中導(dǎo)入XLSX 
import  * as XLSX from 'xlsx' 

API參考

https://www.npmjs.com/package/xlsx

到此這篇關(guān)于vue上傳解析excel文件,列表中輸出內(nèi)容的文章就介紹到這了,更多相關(guān)vue上傳解析excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論