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

vue中如何使用xlsx讀取excel文件實(shí)例代碼

 更新時(shí)間:2023年12月12日 16:00:54   作者:捂耳聽(tīng)楓  
在Vue中使用xlsx庫(kù)可以幫助我們讀取和寫入Excel文件,下面這篇文章主要給大家介紹了關(guān)于vue中如何使用xlsx讀取excel文件的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下

下載安裝插件

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;

屬性屬性值描述
lastModifiedNumber表示最近一次的修改時(shí)間的時(shí)間戳
lastModeifiedDateObject表示最后一次修改時(shí)間的Date對(duì)象,可以在其中調(diào)用Date對(duì)象的有關(guān)方法。
name文件上傳時(shí)的文件名
size文件的字節(jié)大小
typeString文件的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方法讀取
binaryBinatyString格式(byte n is data.charCodeAt (n))
stringUTF-8編碼的字符串
buffernodejs Buffer
arrayUint8Array,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)文章

最新評(píng)論