vue中如何使用xlsx讀取excel文件實(shí)例代碼
下載安裝插件
npm install xlsx or yarn add xlsx
在項(xiàng)目的node_modules文件夾和package.json文件中可以找到xlsx依賴
導(dǎo)入項(xiàng)目
import * as XLSX from "../../node_modules/xlsx"
獲取文件對(duì)象
這里使用的h5原生文件上傳項(xiàng)
<input type="file" id="uploadExcel" multiple @change="Change" />
其中multiple
屬性允許上傳多個(gè)文件
在選擇文件的過(guò)程中,觸發(fā)事件的流程是下面這樣的:
首先觸發(fā)的是鼠標(biāo)按下事件mousedown,然后就是焦點(diǎn)到了input上面,然后鼠標(biāo)抬起事件mouseup,觸發(fā)click事件,失去焦點(diǎn)onblur以后彈出文件選擇框,選中文件以后觸發(fā)焦點(diǎn)focus,最后觸發(fā)change事件。
其中,可以發(fā)現(xiàn)fileList是一個(gè)類數(shù)組,由傳入的file對(duì)象組成。每個(gè)file對(duì)象包含一下屬性:
打印fileList信息
let fileList = event.target.files;
屬性 | 屬性值 | 描述 |
---|---|---|
lastModified | Number | 表示最近一次的修改時(shí)間的時(shí)間戳 |
lastModeifiedDate | Object | 表示最后一次修改時(shí)間的Date對(duì)象,可以在其中調(diào)用Date對(duì)象的有關(guān)方法。 |
name | 文件上傳時(shí)的文件名 | |
size | 文件的字節(jié)大小 | |
type | String | 文件的MIME類型 |
weblitRelativePath | 當(dāng)在input上添加webkitdirectory屬性時(shí),可選文件夾,此時(shí)weblitRelativePath表示文件夾中文件的相對(duì)路徑。未加時(shí)為空 |
創(chuàng)建一個(gè)FileReader實(shí)例:
let reader = new FileReader();
FileReader提供了如下方法:
方法定義 | 方法定義 |
---|---|
readAsArrayBuffer(file) | 按字節(jié)讀取文件內(nèi)容,結(jié)果用ArrayBuffer對(duì)象表示 |
readAsBinaryString(file) | 按字節(jié)讀取文件內(nèi)容,結(jié)果為文件的二進(jìn)制串 |
readAsDataURL(file) | 將文件讀取為DataURL,因此可以讀取圖片。 |
readAsText(file, encoding) | 將文件讀取為文本,第二個(gè)參數(shù)是文本的編碼方式,默認(rèn)為utf-8 |
abort() | 終止文件讀取操作 |
FileReader提供了如下 事件:
事件 | 描述 |
---|---|
onabort | 當(dāng)讀取操作被終止時(shí)調(diào)用 |
onerror | 當(dāng)讀取操作發(fā)生錯(cuò)誤時(shí)調(diào)用 |
onload | 當(dāng)讀取操作成功完成時(shí)調(diào)用 |
onloaded | 當(dāng)讀取操作完成時(shí)調(diào)用,無(wú)論成功或失敗 |
onloadstart | 當(dāng)讀取操作開(kāi)始時(shí)調(diào)用 |
onprogress | 在讀取數(shù)據(jù)過(guò)程中周期性調(diào)用 |
讀取excel主要是通過(guò)以下語(yǔ)句實(shí)現(xiàn):
XLSX.read(data, { type: type });
返回的對(duì)象
FileReader方法對(duì)應(yīng)的type取值如下:
base64 | 以base64方法讀取 |
binary | BinatyString格式(byte n is data.charCodeAt (n)) |
string | UTF-8編碼的字符串 |
buffer | nodejs Buffer |
array | Uint8Array,8位無(wú)符號(hào)數(shù)組 |
file | 文件的路徑(僅nodejs下支持) |
vue中的v-for()展示數(shù)據(jù)
演示
代碼
<template> <div class="container"> <div class="header"> <div class="btn"> <button @click="Add">添加</button> <button>保存</button> <button @click="AllAdd">全部刪除</button> </div> <div class="inp"> <input type="file" id="uploadExcel" multiple @change="Change" /> </div> </div> <table id="table"> <thead> <tr> <th>學(xué)號(hào)</th> <th>姓名</th> <th>年齡</th> <th>班級(jí)</th> <th>操作</th> </tr> </thead> <tbody style="text-align: center"> <tr v-for="item in dataList" :key="item.id"> <td>{{ item.__EMPTY }}</td> <td>{{ item.__EMPTY_1 }}</td> <td>{{ item.__EMPTY_2 }}</td> <td>{{ item.__EMPTY_3 }}</td> <td> <button @click="del(item.__EMPTY)">刪除</button> <button @click="Add()">修改</button> </td> </tr> </tbody> </table> <div id="shade" class="c1 hide"></div> <div id="modal" class="c2 hide"> <h2>學(xué)生信息</h2> <p>學(xué)號(hào)<input type="text" /></p> <p>姓名<input type="text" /></p> <p>年齡<input type="text" /></p> <p>班級(jí)<input type="text" /></p> <p> <button type="button">添加</button> <button type="button" @click="Hide();">取消</button> </p> </div> </div> </template> <script> import * as XLSX from "../../node_modules/xlsx" export default { data() { return { dataList: [] }; }, methods: { Change(event) { // 獲取到文件夾 let fileList = event.target.files; // 如果數(shù)據(jù)不為空 if (fileList) { // 前言:FileReader是一種異步文件讀取機(jī)制,結(jié)合input:file可以很方便的讀取本地文件。 let reader = new FileReader(); let file = fileList[0]; //拿到第一條數(shù)據(jù) reader.readAsBinaryString(file)// 將文件以二進(jìn)制形式讀入頁(yè)面 console.log(this); //這里的this指向 vue中的data let _this = this //把data里的數(shù)據(jù)賦值給新的變量 // wb:wordbook 工作表 reader.addEventListener("load", function (e) { console.log(this); //FileReader實(shí)例對(duì)象 var data = e.target.result; //讀取成功后result中的數(shù)據(jù) var wb = XLSX.read(data, { type: 'binary' }); //以base64方法讀取 結(jié)果 let sheetName = wb.SheetNames[0] //是獲取Sheets中第一個(gè)Sheet的名字 let sheets = wb.Sheets[sheetName] //wb.Sheets[Sheet名]獲取第一個(gè)Sheet的數(shù)據(jù) //將數(shù)據(jù)解析為json字符串 let dataList2 = JSON.stringify(XLSX.utils.sheet_to_json(sheets)) let dataList3 = (JSON.parse(dataList2)) //講json轉(zhuǎn)為 數(shù)組的格式 看格式所需 _this.dataList = dataList3//賦值 }) } }, Add() { document.getElementById('shade').classList.remove('hide'); document.getElementById('modal').classList.remove('hide'); }, Hide() { document.getElementById('shade').classList.add('hide'); document.getElementById('modal').classList.add('hide'); }, AllAdd() { }, del(id) { console.log(id); let index = null index = this.dataList.findIndex(item => { if (item.id === id) return true }) this.dataList.splice(index, 1) }, }, }; </script> <style scoped> .container { width: 800px; margin: 0 auto; } .header { display: flex; justify-content: space-between; margin-bottom: 10px; } table { width: 100%; border-collapse: collapse; border: 1px solid; } tr, th, td { border: 1px solid #000; padding: 5px; } button { border: none; padding: 5px; background-color: #00a297; color: #fff; border-radius: 5px; cursor: pointer; margin: 0 5px; } tr:nth-child(2n) { background-color: #dcdcdc; } .hide { display: none; } .c1 { position: fixed; top: 0; bottom: 0; left: 0; right: 0; background: rgba(0, 0, 0, .5); z-index: 2; } .c2 { background-color: white; position: fixed; width: 400px; height: 300px; top: 50%; left: 50%; z-index: 3; margin-top: -150px; margin-left: -200px; } </style>
總結(jié)
到此這篇關(guān)于vue中如何使用xlsx讀取excel文件的文章就介紹到這了,更多相關(guān)vue xlsx讀取excel文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解VS Code使用之Vue工程配置format代碼格式化
這篇文章主要介紹了詳解VS Code使用之Vue工程配置format代碼格式化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03vue-router 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由衛(wèi)士)的實(shí)例代碼
這篇文章主要介紹了vue-router 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由衛(wèi)士)的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09Vue實(shí)現(xiàn)兩種路由權(quán)限控制方式
路由權(quán)限控制常用于后臺(tái)管理系統(tǒng)中,對(duì)不同業(yè)務(wù)人員能夠訪問(wèn)的頁(yè)面進(jìn)行一個(gè)權(quán)限的限制。本文主要介紹了兩種Vue 路由權(quán)限控制,具有一定的參考價(jià)值,感興趣的可以了解一下2021-10-10vue-cli2與vue-cli3在一臺(tái)電腦共存的實(shí)現(xiàn)方法
這篇文章主要介紹了vue-cli2與vue-cli3在一臺(tái)電腦共存的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Vue配置marked鏈接添加target="_blank"的方法
這篇文章主要介紹了Vue配置marked鏈接添加target="_blank"的方法,文中給大家提到了vue實(shí)現(xiàn)類似target="_blank"打開(kāi)新窗口的代碼,感興趣的朋友參考下吧2019-07-07基于Vue+elementUI實(shí)現(xiàn)動(dòng)態(tài)表單的校驗(yàn)功能(根據(jù)條件動(dòng)態(tài)切換校驗(yàn)格式)
這篇文章主要介紹了Vue+elementUI的動(dòng)態(tài)表單的校驗(yàn)(根據(jù)條件動(dòng)態(tài)切換校驗(yàn)格式),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04vue實(shí)現(xiàn)todolist功能、todolist組件拆分及todolist的刪除功能
這篇文章主要介紹了vue實(shí)現(xiàn)todolist功能、todolist組件拆分及todolist的刪除功能,需要的朋友可以參考下2019-04-04vue實(shí)現(xiàn)購(gòu)物車的小練習(xí)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)購(gòu)物車的小練習(xí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12詳解vue 自定義marquee無(wú)縫滾動(dòng)組件
這篇文章主要介紹了vue自定義marquee無(wú)縫滾動(dòng)組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04