国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue2 - 基于Element UI實(shí)現(xiàn)上傳Excel表單數(shù)據(jù)功能

這篇具有很好參考價(jià)值的文章主要介紹了vue2 - 基于Element UI實(shí)現(xiàn)上傳Excel表單數(shù)據(jù)功能。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、項(xiàng)目場景

批量數(shù)據(jù)上傳后臺(tái),需要從后臺(tái)下載一個(gè)固定格式的 Excel表格,然后在表格里面添加數(shù)據(jù),將數(shù)據(jù)格式化,再上傳給后臺(tái),后臺(tái)做解析處理,往數(shù)據(jù)庫添加數(shù)據(jù)

二、實(shí)現(xiàn)功能展示

點(diǎn)擊導(dǎo)入excel按鈕,跳轉(zhuǎn)到上傳excel功能頁面,點(diǎn)擊上傳或者是通過拖拽都能實(shí)現(xiàn)excel表格上傳
vue上傳excel,vue+組件庫,ui,excel,javascript,vue

三、實(shí)現(xiàn)思路

通過Element UI的<el-dialog>實(shí)現(xiàn)彈出層

1、excel按鈕

:isShow=isShow是否顯示上傳文件層
:onSuccess="success"上傳成功之后的回調(diào)

2、excel上傳頁面

accept=".xlsx, .xls":限定文件類型
beforeUpload(){}在上傳之前做一些自己的特殊判斷,如判斷文件的大小是否大于 1 兆?若大于 1 兆則停止解析并提示錯(cuò)誤信息。

四、實(shí)現(xiàn)代碼

1、下載xlsx

excel導(dǎo)入功能需要使用npm包xlsx,所以需要安裝**xlsx**插件

npm i xlsx

2、頁面代碼

excel按鈕頁面

<template>
  <div>
    <el-button
      type="primary"
      @click="goExcel"
      style="margin: 50px 50px 50px 50px"
      >導(dǎo)入excel表格</el-button
    >
    <UploadExcel :onSuccess="success" :isShow="isShow"></UploadExcel>
  </div>
</template>
<script>
import UploadExcel from "./components/UploadExcel.vue";
export default {
  name: "App",
  components: { UploadExcel },
  data() {
    return {
      isShow: false,
    };
  },
  methods: {
    goExcel() {
      this.isShow = true;
    },
    async success(data) {
      // 數(shù)據(jù)庫的key為英文,我們上傳的key為中文,需要一一轉(zhuǎn)化
      const userRelations = {
        入職日期: "timeOfEntry",
        手機(jī)號: "mobile",
        姓名: "username",
        轉(zhuǎn)正日期: "correctionTime",
        工號: "workNumber",
      };
      // 將key值一一對應(yīng)
      const newArr = data.results.map((item) => {
        var userInfo = {};
        Object.keys(item).forEach((key) => {
          userInfo[userRelations[key]] = item[key];
        });
        return userInfo;
      });
      console.log(newArr);
      //   await importEmployee(newArr); // 調(diào)用上傳接口
      this.$message("上傳文件成功");
      this.isShow = false;
    },
  },
};
</script>

UploadExcel頁面

<template>
  <el-dialog title="導(dǎo)入excel表格" :visible="isShow" @close="closeShow">
    <div class="upload-excel">
      <div class="btn-upload">
        <el-button
          :loading="loading"
          size="mini"
          type="primary"
          @click="handleUpload"
        >
          點(diǎn)擊上傳
        </el-button>
      </div>

      <input
        ref="excel-upload-input"
        class="excel-upload-input"
        type="file"
        accept=".xlsx, .xls"
        @change="handleClick"
      />
      <div
        class="drop"
        @drop="handleDrop"
        @dragover="handleDragover"
        @dragenter="handleDragover"
      >
        <i class="el-icon-upload" />
        <span>將文件拖到此處</span>
      </div>
    </div>
  </el-dialog>
</template>


<script>
import * as XLSX from "xlsx";
export default {
  props: {
    isShow: Boolean,
    onSuccess: Function, // eslint-disable-line
  },
  data() {
    return {
      loading: false,
      excelData: {
        header: [],
        results: [],
      },
    };
  },
  methods: {
    closeShow() {
      this.$parent.isShow = false;
    },
    // 點(diǎn)擊導(dǎo)入
    handleDrop(e) {
      e.stopPropagation();
      e.preventDefault();
      if (this.loading) return;
      const files = e.dataTransfer.files;
      if (files.length !== 1) {
        this.$message.error("僅支持單個(gè)上傳一個(gè)文件");
        return;
      }
      const rawFile = files[0]; // 獲取文件信息
      if (!this.isExcel(rawFile)) {
        //是不是excel文件
        this.$message.error(
          "Only supports upload .xlsx, .xls, .csv suffix files"
        );
        return false;
      }
      this.upload(rawFile);
      e.stopPropagation();
      e.preventDefault();
    },
    handleDragover(e) {
      e.stopPropagation();
      e.preventDefault();
      e.dataTransfer.dropEffect = "copy";
    },
    handleUpload() {
      this.$refs["excel-upload-input"].click(); //觸發(fā)表單上傳事件,跳出選擇文件框
    },
    handleClick(e) {
      const files = e.target.files;
      const rawFile = files[0]; // only use files[0]
      if (!rawFile) return;
      this.upload(rawFile);
    },
    upload(rawFile) {
      this.$refs["excel-upload-input"].value = null; // fix can't select the same excel
      if (!this.beforeUpload) {
        this.readerData(rawFile);
        return;
      }
      const before = this.beforeUpload(rawFile); //判斷文件上傳大小
      if (before) {
        this.readerData(rawFile); //把excel轉(zhuǎn)化成數(shù)組
      }
    },
    // 限定文件大小
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1;
      if (isLt1M) {
        return true;
      }
      this.$message({
        message: "請不要上傳大于1M的文件",
        type: "warning",
      });
      return false;
    },

    readerData(rawFile) {
      this.loading = true;
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsArrayBuffer(rawFile);
        //參數(shù)可以是 Blob 或者 File 對象異步結(jié)果將在onload 事件中體現(xiàn)
        reader.onload = (e) => {
          const data = e.target.result;
          const workbook = XLSX.read(data, { type: "array" });
          const firstSheetName = workbook.SheetNames[0]; // "SheetJS" 取出工作表名稱
          const worksheet = workbook.Sheets[firstSheetName]; //取出工作表名稱對應(yīng)的表格數(shù)據(jù)
          const header = this.getHeaderRow(worksheet); //表頭名
          const results = XLSX.utils.sheet_to_json(worksheet); //輸出數(shù)據(jù),除去表頭
          this.excelData.results = results;
          this.excelData.header = header;
          this.onSuccess && this.onSuccess(this.excelData);
          this.loading = false;
          resolve();
        };
      });
    },
    getHeaderRow(sheet) {
      const headers = [];
      const range = XLSX.utils.decode_range(sheet["!ref"]); //!ref: "A1:E21"
      let C;
      const R = range.s.r;
      /* start in the first row */
      for (C = range.s.c; C <= range.e.c; ++C) {
        /* walk every column in the range */
        const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })];
        /* find the cell in the first row */
        let hdr = "UNKNOWN " + C; // <-- replace with your desired default
        if (cell && cell.t) hdr = XLSX.utils.format_cell(cell); //每個(gè)頭部名
        headers.push(hdr);
      }
      return headers;
    },
    isExcel(file) {
      return /\.(xlsx|xls|csv)$/.test(file.name);
    },
  },
};
</script>
<style scoped lang="scss">
.upload-excel {
  display: flex;
  justify-content: center;
  // margin-top: 100px;
  .excel-upload-input {
    display: none;
    z-index: -9999;
  }
  .btn-upload,
  .drop {
    border: 1px dashed #bbb;
    width: 350px;
    height: 160px;
    text-align: center;
    line-height: 160px;
  }
  .drop {
    line-height: 80px;
    color: #bbb;
    i {
      font-size: 60px;
      display: block;
    }
  }
}
</style>

五、實(shí)現(xiàn)效果

vue上傳excel,vue+組件庫,ui,excel,javascript,vue文章來源地址http://www.zghlxwxcb.cn/news/detail-534579.html

到了這里,關(guān)于vue2 - 基于Element UI實(shí)現(xiàn)上傳Excel表單數(shù)據(jù)功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • springboot+vue2 實(shí)現(xiàn)文件上傳,vue表單實(shí)現(xiàn)上傳多張照片或視頻回顯

    springboot+vue2 實(shí)現(xiàn)文件上傳,vue表單實(shí)現(xiàn)上傳多張照片或視頻回顯

    此教程可以實(shí)現(xiàn)對上傳的文件管理,并加入了我的功能需求,大家可以自行進(jìn)行修改。 文件管理: 功能需求視頻: 文件上傳視頻(圖片和視頻) 先來簡單介紹下功能實(shí)現(xiàn),實(shí)現(xiàn)此功能分為數(shù)據(jù)庫、后端java、前端vue共同實(shí)現(xiàn); 業(yè)務(wù)流程: vue頁面點(diǎn)擊上傳后,通過調(diào)用后端接

    2024年02月04日
    瀏覽(32)
  • 若依前后端分離+Vue2+Element UI實(shí)現(xiàn)根據(jù)列數(shù)據(jù)展示不同頁面的數(shù)據(jù)

    若依前后端分離+Vue2+Element UI實(shí)現(xiàn)根據(jù)列數(shù)據(jù)展示不同頁面的數(shù)據(jù)

    多個(gè)表格中存在同一字段,并且可通過該字段查到與之對應(yīng)的數(shù)據(jù)。舉個(gè)簡單的例子,比如我有一個(gè)學(xué)生表、一個(gè)老師表、一個(gè)課程表,假設(shè)老師表和學(xué)生表里都有課程ID這一字段,那么我可以在課程表里通過課程ID來,查找需要上這門課的學(xué)生,以及教這門課的老師,并且在

    2024年01月19日
    瀏覽(26)
  • vue2 element ui 實(shí)現(xiàn)圖標(biāo)下拉選擇

    vue2 element ui 實(shí)現(xiàn)圖標(biāo)下拉選擇

    1.展示效果 代碼展示: (1)封裝icon.js (2) 在需要使用的頁面復(fù)制進(jìn)去 (3)在自己需要使用的地方復(fù)制進(jìn)去 (4)css樣式部分 (5)初始化數(shù)據(jù)

    2024年02月10日
    瀏覽(27)
  • Vue2 +Element-ui實(shí)現(xiàn)前端頁面

    Vue2 +Element-ui實(shí)現(xiàn)前端頁面

    以一個(gè)簡單的前端頁面為例。主要是利用vue和element-ui實(shí)現(xiàn)。 里面涉及的主要包括:新建vue項(xiàng)目、一行多個(gè)輸入框、頁面實(shí)現(xiàn)等。 ? ①首先安裝nodejs,這部分在此就不講啦。 ②然后安裝vue-cli: 查看是否安裝成功: 安裝成功之后就輸出vue的版本 ③在cmd窗口新建一個(gè)vue2腳手架

    2024年02月16日
    瀏覽(33)
  • 基于vue+Element UI的文件上傳(可拖拽上傳)

    基于vue+Element UI的文件上傳(可拖拽上傳)

    drag: 支持拖拽上傳 action:必選參數(shù),上傳的地址 ref:這里主要是用于文件上傳完成后清除文件的 on-remove:文件列表移除文件時(shí)的鉤子 auto-upload:是否在選取文件后立即進(jìn)行上傳 on-change:文件狀態(tài)改變時(shí)的鉤子,添加文件、上傳成功和上傳失敗時(shí)都會(huì)被調(diào)用 注:這里使用的

    2023年04月08日
    瀏覽(30)
  • Vue + Element ui 實(shí)現(xiàn)動(dòng)態(tài)表單,包括新增行/刪除行/動(dòng)態(tài)表單驗(yàn)證/提交功能

    Vue + Element ui 實(shí)現(xiàn)動(dòng)態(tài)表單,包括新增行/刪除行/動(dòng)態(tài)表單驗(yàn)證/提交功能

    原創(chuàng)/朱季謙 最近通過Vue + Element ui實(shí)現(xiàn)了動(dòng)態(tài)表單功能,該功能還包括了動(dòng)態(tài)表單新增行、刪除行、動(dòng)態(tài)表單驗(yàn)證、動(dòng)態(tài)表單提交功能,趁熱打鐵,將開發(fā)心得記錄下來,方便以后再遇到類似功能時(shí),直接拿來應(yīng)用。 簡化的頁面效果圖如下: 最開始,我是用了純粹的表格形

    2024年02月04日
    瀏覽(26)
  • vue2+element-ui實(shí)現(xiàn)側(cè)邊導(dǎo)航欄

    編寫 client/src/components/LeftMenu.vue ,創(chuàng)建側(cè)邊導(dǎo)航欄: 編輯 client/src/views/Index.vue ,引入側(cè)邊導(dǎo)航欄:

    2024年02月02日
    瀏覽(30)
  • vue2+element-ui 實(shí)現(xiàn)國際化

    在src目錄下創(chuàng)建一個(gè)lang文件夾,同時(shí)創(chuàng)建zh.js(中文),en.js(英文),ja.js(日文),fr.js(法文)四個(gè)語言包js文件,并創(chuàng)建一個(gè)index.js文件,用來整合語言包 對于一個(gè)項(xiàng)目來說,一個(gè)語言包需要包含所有頁面以及組件;在語言包以頁面為單位,創(chuàng)建一個(gè)對象;對公共的title或者按鈕名

    2024年02月02日
    瀏覽(30)
  • element ui表單上傳文件

    實(shí)現(xiàn)提交表單和上傳文件同步進(jìn)行,把表單數(shù)據(jù)作為upload組件上傳附帶的額外參數(shù)。 methods: ?

    2024年02月16日
    瀏覽(26)
  • vue2&Element-ui實(shí)現(xiàn)表格單元格合并

    vue2&Element-ui實(shí)現(xiàn)表格單元格合并

    由于項(xiàng)目需要實(shí)現(xiàn)單元格合并目前只是單頁沒有做分頁處理先上效果圖 看下數(shù)據(jù)結(jié)構(gòu) Element table提供的api arraySpanMethod columnIndex=0表示從第一列開始 rowIndex表示需要操作的行數(shù) 同濟(jì)醫(yī)院加上合計(jì)有12行從0開始=11 判斷條件是rowIndex余12===0 我們打印一下 或者改成 表示從0開始到1

    2024年02月12日
    瀏覽(37)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包