最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實現(xiàn)excel文件的導(dǎo)入和讀取完整步驟

 更新時間:2023年10月13日 15:46:32   作者:bu_xiang_tutou  
Vue的數(shù)據(jù)綁定功能非常強大,很適合用來讀取Excel內(nèi)容,這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)excel文件的導(dǎo)入和讀取的相關(guān)資料,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下

1.效果展示

上傳數(shù)據(jù)前

 上傳數(shù)據(jù)后

或者

2.下載

npm install xlsx@0.17.0

如果一直報關(guān)于xlsx的read的錯誤,這里是因為xlsx的0.18.0版本已經(jīng)沒有read屬性了,所以最好是使用0.18.0版本以下的xlsx。

3. js文件

excel.js

import { stringify } from "json5";
// 按照二進制讀取文件
export function readFile(file) {
  return new Promise((resolve) => {
    let reader = new FileReader();
    reader.readAsBinaryString(file);
    reader.onload = (e) => {
      resolve(e.target.result);
    };
  });
}
// 字段對應(yīng)表
export let character = {
  staffName: {
    text: "員工姓名",
    type: "string",
  },
  sex: {
    text: "性別",
    type: "string",
  },
  idNumber: {
    text: "身份證號",
    type: "string",
  },
  staffEmail: {
    text: "員工郵箱",
    type: "string",
  },
  phone: {
    text: "電話號碼",
    type: "string",
  },
  jobPreference: {
    text: "工作偏好",
    type: "string",
  },
  timePreference: {
    text: "時間偏好",
    type: "string",
  },
  datePreference: {
    text: "時間段偏好",
    type: "string",
  }
};
// 時間字符串格式化
export function formatTime(str, tpl) {
  let arr = str.match(/\d+/g).map((item) => {
    return item.length < 2 ? "0" + item : item;
  });
  tpl = tpl || "{0}年{1}月{2}日 {3}時{4}分{5}秒";
  return tpl.replace(/\{(\d+)\}/g, (_, group) => {
    return arr[group] || "00";
  });
}

utils.js 實現(xiàn)加載

// 設(shè)置異步延遲間隔
export function delay(interval = 0) {
    return new Promise((resolve) => {
      let timer = setTimeout((_) => {
        clearTimeout(timer);                                                                                                          
        resolve();
      }, interval);
    });
  }

4 .界面處理

<template>
  <el-upload
    ref="upload"
    class="upload-demo"
    action
    accept=".xlsx,.xls"
    :show-file-list="false"
    :on-exceed="handleExceed"
    :auto-upload="false"
    :on-change="handle"
  >
    <template #trigger>
      <el-button type="primary">選擇文件</el-button>
    </template>
  </el-upload>
  <el-table :data="employeeData" border style="width: 100%">
    <el-table-column prop="staffName" label="姓名" width="100px" />
    <el-table-column prop="sex" label="性別" width="80px" />
    <el-table-column prop="idNumber" label="身份證號" width="200px" />
    <el-table-column prop="phone" label="電話號碼" width="150px" />
    <el-table-column prop="staffEmail" label="郵箱" width="200px" />
    <el-table-column prop="jobPreference" label="工作偏好" />
    <el-table-column prop="timePreference" label="時間偏好" />
    <el-table-column prop="datePreference" label="時間段偏好" />
    <el-table-column label="操作">
        <el-button>編輯</el-button>
        <el-button type="danger">刪除</el-button>
    </el-table-column>
  </el-table>
</template>
<script>
//excel文件的處理
import { character, readFile } from "@/assets/js/excel";
//異步消息處理
import { delay } from "@/assets/js/utils";
//excel文件數(shù)據(jù)解析
import XLSX from "xlsx";
import { ElLoading } from 'element-plus'
export default {
  data() {
    return {
      employeeData: [],
    };
  },
  component: {},
  created() {
  },
  methods: {
    //采集excel數(shù)據(jù)
    async handle(ev) {
      //這個是上傳的文件
      let file = ev.raw;
      // console.log(file, "file");
      //沒有文件
      if (!file) return;
      //解析和上傳到后端的時候進行l(wèi)oading加載顯示
      let loadingInstance =  ElLoading.service({
        text: "請稍等一下,數(shù)據(jù)正在處理中"
      });
      await delay(100);//延遲
      //讀取file中的數(shù)據(jù)
      //把文件解析成二進制數(shù)據(jù),把二級制數(shù)據(jù)變成excel表格式的數(shù)據(jù)
      let data = await readFile(file);
      let workbook = XLSX.read(data, { type: "binary" });
      //拿到第一個sheet表的數(shù)據(jù),把第一個表格的數(shù)據(jù)轉(zhuǎn)換成JSON數(shù)據(jù)
      const worksheet = workbook.Sheets[workbook.SheetNames[0]];
      data = XLSX.utils.sheet_to_json(worksheet);
      //把讀取出來的數(shù)據(jù)變成最后可以傳遞給服務(wù)器的數(shù)據(jù)
      let employees = [];
      data.forEach((item) => {
        let obj = {};
        for (let key in character) {
          if (!character.hasOwnProperty(key)) break;
          let v = character[key];
          const text = v.text;
          const type = v.type;
          v = item[text] || "";
          //對數(shù)據(jù)類型的處理
          type === "string" ? (v = String(v)) : null;
          type === "number" ? (v = Number(v)) : null;
          obj[key] = v;
        }
        employees.push(obj);
      });
      console.log(employees, "最后傳入后端的excel數(shù)據(jù)");
      this.employeeData = employees;
      loadingInstance.close();
    },
  },
};
</script>

總結(jié)

到此這篇關(guān)于vue實現(xiàn)excel文件的導(dǎo)入和讀取的文章就介紹到這了,更多相關(guān)vue導(dǎo)入和讀取excel文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue3生命周期函數(shù)使用及說明(掛載、更新、銷毀)

    vue3生命周期函數(shù)使用及說明(掛載、更新、銷毀)

    文章對比了Vue2和Vue3的生命周期函數(shù),指出Vue3在Vue2的基礎(chǔ)上增加了on前綴的生命周期,移除了了創(chuàng)建前后的生命周期鉤子,并引入了setup和onUnmount等等新特性,文章強調(diào)了在單頁面中生命函數(shù)可以多次使用,并提供了表格對比幫助理解
    2026-05-05
  • vue用h()函數(shù)創(chuàng)建Vnodes的實現(xiàn)

    vue用h()函數(shù)創(chuàng)建Vnodes的實現(xiàn)

    Vue提供了一個h()函數(shù)用于創(chuàng)建vnodes,本文就來介紹一下vue用h()函數(shù)創(chuàng)建Vnodes的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • vue3中unplugin-auto-import自動引入示例代碼

    vue3中unplugin-auto-import自動引入示例代碼

    unplugin-auto-import 這個插件是為了解決在開發(fā)中的導(dǎo)入問題,下面這篇文章主要給大家介紹了關(guān)于vue3中unplugin-auto-import自動引入的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • Vue.js項目在IE11白屏報錯的解決方法

    Vue.js項目在IE11白屏報錯的解決方法

    本文主要介紹了Vue.js項目在IE11白屏報錯的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-08-08
  • VueJS頁面渲染之后如何調(diào)用函數(shù)

    VueJS頁面渲染之后如何調(diào)用函數(shù)

    這篇文章主要介紹了VueJS頁面渲染之后如何調(diào)用函數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • VUE3基于vite封裝條形碼和二維碼組件的詳細過程

    VUE3基于vite封裝條形碼和二維碼組件的詳細過程

    基礎(chǔ)組件開發(fā)是項目業(yè)務(wù)開發(fā)的基石, 本文主要介紹了通過vue3的vite腳手架快速搭建項目, 開發(fā)條形碼和二維碼組件的過程,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • vue實現(xiàn)學生信息管理系統(tǒng)

    vue實現(xiàn)學生信息管理系統(tǒng)

    這篇文章主要為大家詳細介紹了vue實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 完美解決vue中報錯?“TypeError:?Cannot?read?properties?of?null?(reading'forEach')“

    完美解決vue中報錯?“TypeError:?Cannot?read?properties?of?null?

    這篇文章主要介紹了完美解決vue中報錯?“TypeError:?Cannot?read?properties?of?null?(reading?‘forEach‘)“,本文給大家分享詳細解決方案,需要的朋友可以參考下
    2023-02-02
  • Vue移動端實現(xiàn)圖片上傳及超過1M壓縮上傳

    Vue移動端實現(xiàn)圖片上傳及超過1M壓縮上傳

    這篇文章主要為大家詳細介紹了Vue移動端實現(xiàn)圖片上傳及超過1M壓縮上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • vue中使用v-for時為什么不能用index作為key

    vue中使用v-for時為什么不能用index作為key

    這篇文章主要介紹了vue中使用v-for時為什么不能用index作為key,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04

最新評論

仙桃市| 旬邑县| 怀来县| 和静县| 崇阳县| 长汀县| 邳州市| 剑川县| 交城县| 井陉县| 漠河县| 紫金县| 云龙县| 东乡| 禹城市| 江孜县| 开鲁县| 江源县| 社旗县| 阿图什市| 仁寿县| 肥城市| 巫溪县| 濮阳县| 新河县| 页游| 阳泉市| 抚松县| 饶平县| 施秉县| 周口市| 九寨沟县| 泾阳县| 保德县| 淮安市| 陵水| 司法| 灌云县| 谷城县| 雷波县| 来宾市|