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

示例源代碼(共105行)
/*
* @Author: 大劍師蘭特(xiaozhuanlan),還是大劍師蘭特(CSDN)
* @此源代碼版權(quán)歸大劍師蘭特所有,可供學(xué)習(xí)或商業(yè)項(xiàng)目中借鑒,未經(jīng)授權(quán),不得重復(fù)地發(fā)表到博客、論壇,問答,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="班級" width="180"></el-table-column>
<el-table-column prop="score" label="成績"></el-table-column>
<el-table-column prop="level" label="等級"></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ò)誤!請重新選擇')
return
}
this.fileExchange(file).then(tabJson => {
if (tabJson && tabJson.length > 0) {
this.xlsxData = tabJson[0].sheet
console.log(this.xlsxData)
// xlsxJson就是解析出來的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>安裝引用依賴
// 安裝插件 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前端Vue通過Minio返回的URL下載文件實(shí)現(xiàn)方法
Minio是一個(gè)靈活、高性能、開源的對象存儲解決方案,適用于各種存儲需求,并可以與云計(jì)算、容器化、大數(shù)據(jù)和應(yīng)用程序集成,這篇文章主要給大家介紹了關(guān)于前端Vue通過Minio返回的URL下載文件實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-07-07
vue動(dòng)態(tài)生成新表單并且添加驗(yàn)證校驗(yàn)規(guī)則方式
這篇文章主要介紹了vue動(dòng)態(tài)生成新表單并且添加驗(yàn)證校驗(yàn)規(guī)則方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
淺析vue偵測數(shù)據(jù)的變化之基本實(shí)現(xiàn)
這里涉及到Vue一個(gè)重要特性:響應(yīng)式系統(tǒng)。數(shù)據(jù)模型只是普通的 JavaScript對象,當(dāng)我們修改時(shí),視圖會(huì)被更新,而變化偵測是響應(yīng)式系統(tǒng)的核心2021-06-06
vue-print-nb實(shí)現(xiàn)頁面打印功能實(shí)例(含分頁打印)
在項(xiàng)目中,有時(shí)需要打印頁面的表格,在網(wǎng)上找了一個(gè)打印組件vue-print-nb,用了還不錯(cuò),所以下面這篇文章主要給大家介紹了關(guān)于vue-print-nb實(shí)現(xiàn)頁面打印功能的相關(guān)資料,需要的朋友可以參考下2022-08-08
Vue3?接入?i18n?實(shí)現(xiàn)國際化多語言案例分析
在?Vue.js?3?中實(shí)現(xiàn)網(wǎng)頁的國際化多語言,最常用的包是?vue-i18n,通常我們會(huì)與?vue-i18n-routing?一起使用,這篇文章主要介紹了Vue3?如何接入?i18n?實(shí)現(xiàn)國際化多語言,需要的朋友可以參考下2024-07-07
Vue如何實(shí)現(xiàn)自動(dòng)觸發(fā)功能
這篇文章主要介紹了Vue如何實(shí)現(xiàn)自動(dòng)觸發(fā)功能,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
vue-router結(jié)合vuex實(shí)現(xiàn)用戶權(quán)限控制功能
這篇文章主要介紹了vue-router結(jié)合vuex實(shí)現(xiàn)用戶權(quán)限控制功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11

