vue +elementui 導(dǎo)入CSV文件的方式
1. 安裝依賴(lài)
cnpm i papaparse -s
cnpm i jschardet -s
2. 封裝一個(gè)公共js方法,使用papaparse解析CSV文件且返回?cái)?shù)組格式
import jschardet from 'jschardet' import Papa from 'papaparse' /** * csv file to 2D arr * */ // 檢查編碼,引用了 jschardet function checkEncoding(base64Str) { // 這種方式得到的是一種二進(jìn)制串 var str = atob(base64Str.split(';base64,')[1]) // console.log(str); // 要用二進(jìn)制格式 var encoding = jschardet.detect(str) encoding = encoding.encoding // console.log( encoding ); if (encoding === 'windows-1252') { // 有時(shí)會(huì)識(shí)別錯(cuò)誤(如UTF8的中文二字) encoding = 'ANSI' } return encoding } function csv(file) { return new Promise((resolve, reject) => { // let file = this.$refs.csvData.files[0] const fReader = new FileReader() fReader.readAsDataURL(file) fReader.onload = function (evt) { const data = evt.target.result // console.log( data ); const encoding = checkEncoding(data) // console.log(encoding); // 轉(zhuǎn)換成二維數(shù)組,需要引入Papaparse.js Papa.parse(file, { encoding: encoding, complete: function (results) { // UTF8 \r\n與\n混用時(shí)有可能會(huì)出問(wèn)題 // console.log(results) const res = results.data if (res[res.length - 1] === '') { // 去除最后的空行 res.pop() } resolve(res) } }) } }) } export default { csv }
3. html頁(yè)面
ps: 里面有導(dǎo)入的一部分代碼,不需要就刪除即可。
<template> <div class="testIndex"> <el-row> <el-col :span="12"> <el-button @click="outBtn" type="success" >導(dǎo)出</el-button></el-col> <el-col :span="12"> <el-upload class="upload-demo" action="" :on-change="handleChange" :file-list="fileListUpload" accept=".csv" :auto-upload="false" > <el-button size="small" type="primary" >點(diǎn)擊上傳</el-button> </el-upload> </el-col> </el-row> <el-table :data="tableData" border style="width: 50%" :header-cell-style="{background:'#EEF6FD',color:'#251E25'}" slot="empty" > <el-table-column prop="number" label="#" width="50%" align="center" ></el-table-column> <el-table-column prop="name" label="字段名" align="center" ></el-table-column> <el-table-column prop="nameRemark" label="備注名" align="center" ></el-table-column> <el-table-column prop="index" label="索引字段" align="center" ></el-table-column> </el-table> <!-- 彈框 --> <el-dialog title="數(shù)據(jù)導(dǎo)出" :visible.sync="dialogVisible1" width="30%" > <el-form :model="exportForm" ref="exportForm" label-position="right" > <el-form-item label="導(dǎo)出格式" prop="exportFormat" > <el-select v-model="exportForm.exportFormat" placeholder="請(qǐng)選擇" > <el-option label="CSV" value="0" ></el-option> <el-option label="XML" value="1" ></el-option> <el-option label="HTML" value="2" ></el-option> <el-option label="PDF" value="3" ></el-option> <el-option label="Excel" value="4" ></el-option> </el-select> </el-form-item> </el-form> <div slot="footer" class="dialog-footer" > <el-button @click="handleClose">取 消</el-button> <el-button type="primary" @click="handleExprotData" >確 定</el-button> </div> </el-dialog> </div> </template> <script> import { exportCsv } from "@/utils/testUtils"; import csv2arr from "@/assets/csv-arr"; export default { data() { return { dialogVisible1: false, exportForm: { id: "", exportFormat: "", }, tableData: [ // { id: 1, name: "XXX公司", test1: "測(cè)試11", test2: "測(cè)試22" }, // { id: 2, name: "sdfds", test1: "測(cè)試11-2", test2: "測(cè)試22-2" }, // { id: 3, name: "sff勝多負(fù)少", test1: "測(cè)試11-3", test2: "測(cè)試22-3" }, // { id: 4, name: "兒童", test1: "測(cè)試11-4", test2: "測(cè)試22-4" }, ], fileTemp: null, fileListUpload: [], fileList: [], }; }, methods: { handleClose() { this.dialogVisible1 = false; this.exportForm = { id: "", exportFormat: "", }; }, // 導(dǎo)出按鈕,導(dǎo)出時(shí)觸發(fā)彈窗 outBtn() { this.dialogVisible1 = true; }, //#region 導(dǎo)出功能 handleExprotData() { //表頭 let allColumns = [ { title: "名稱(chēng)", key: "name" }, { title: "測(cè)試1", key: "test1" }, { title: "測(cè)試2", key: "test2" }, ]; let checkboxList = [ { id: 1, name: "XXX公司", test1: "測(cè)試11", test2: "測(cè)試22" }, { id: 2, name: "sdfds", test1: "測(cè)試11-2", test2: "測(cè)試22-2" }, { id: 3, name: "sff勝多負(fù)少", test1: "測(cè)試11-3", test2: "測(cè)試22-3" }, { id: 4, name: "兒童", test1: "測(cè)試11-4", test2: "測(cè)試22-4" }, ]; //this.checkboxList是勾選的需要導(dǎo)出的數(shù)據(jù) console.log("allColumns------", allColumns); console.log("checkboxList----", checkboxList); this.exportForm.exportFormat == 0 && exportCsv(allColumns, checkboxList, "測(cè)試Csv"); }, //#endregion //#region 導(dǎo)入 handleChange(file, fileList) { this.fileTemp = file.raw; if (this.fileTemp) { if ( this.fileTemp.type == ".csv" || this.fileTemp.type == "application/vnd.ms-excel" ) { this.importcsv(file.raw); } else { this.$message({ type: "warning", message: "附件格式錯(cuò)誤,請(qǐng)刪除后重新上傳!", }); } } else { this.$message({ type: "warning", message: "請(qǐng)上傳附件!", }); } }, importcsv(obj) { let _this = this; //如果需要點(diǎn)擊事件結(jié)束之后對(duì)DOM進(jìn)行操作使用)_this.xx=xx進(jìn)行操作 console.log("obj===", obj); // let data = []; csv2arr.csv(obj).then((res) => { console.log("我的數(shù)據(jù)", res); //遍歷csv文件中的數(shù)據(jù),存放到data中 方法不唯一,可自己更改 for (let i = 0; i < res.length - 1; i++) { let obj = {}; obj.number = res[i][0]; obj.name = res[i][1]; obj.nameRemark = res[i][2]; obj.index = res[i][3]; data.push(obj); } console.log("截取第一位之前的數(shù)據(jù)==", data); data.splice(0, 1); //將數(shù)組第一位的表格名稱(chēng)去除 let num = 0; console.log("2024-4-1data==", data); _this.tableData = data; //將數(shù)據(jù)放入要展示的表格中 }); }, //#endregion // csv() { // csv2arr.csv(this.$refs.csvData.files[0]).then((res)=>{ // console.log('我的數(shù)據(jù)', res) // }) // } }, }; </script>
4. 案例圖
ps:目前這個(gè)只適用于CSV文件


到此這篇關(guān)于vue +elementui 導(dǎo)入CSV文件的文章就介紹到這了,更多相關(guān)vue elementui 導(dǎo)入CSV文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 使某個(gè)組件不被 keep-alive 緩存的方法
今天小編就為大家分享一篇vue 使某個(gè)組件不被 keep-alive 緩存的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Vue寫(xiě)一個(gè)簡(jiǎn)單的倒計(jì)時(shí)按鈕功能
這篇文章主要介紹了基于Vue寫(xiě)一個(gè)簡(jiǎn)單的倒計(jì)時(shí)按鈕功能,在項(xiàng)目開(kāi)發(fā)的過(guò)程,經(jīng)常會(huì)遇到發(fā)送驗(yàn)證碼,點(diǎn)擊之后有60秒倒計(jì)時(shí)的按鈕,今天小編就給大家分享實(shí)例代碼,需要的朋友參考下吧2018-04-04vue-cli開(kāi)發(fā)環(huán)境實(shí)現(xiàn)跨域請(qǐng)求的方法
本篇文章主要介紹了vue-cli開(kāi)發(fā)環(huán)境實(shí)現(xiàn)跨域請(qǐng)求的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04vue-cli3自動(dòng)消除console.log()的調(diào)試信息方式
這篇文章主要介紹了vue-cli3自動(dòng)消除console.log()的調(diào)試信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10vue實(shí)現(xiàn)手機(jī)號(hào)碼的校驗(yàn)實(shí)例代碼(防抖函數(shù)的應(yīng)用場(chǎng)景)
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)手機(jī)號(hào)碼的校驗(yàn)的相關(guān)資料,主要是防抖函數(shù)的應(yīng)用場(chǎng)景,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09vue實(shí)現(xiàn)動(dòng)態(tài)列表尾部添加數(shù)據(jù)執(zhí)行動(dòng)畫(huà)
這篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)列表尾部添加數(shù)據(jù)執(zhí)行動(dòng)畫(huà)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04VUE:vuex 用戶(hù)登錄信息的數(shù)據(jù)寫(xiě)入與獲取方式
今天小編就為大家分享一篇VUE:vuex 用戶(hù)登錄信息的數(shù)據(jù)寫(xiě)入與獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11