给了一份表格 让把表格里的数据录到系统里,数据有7百多条,表单还很长 所以简单写了一个脚本 方便快速录入数据,主要用到了 js 读取表单
首先
文件上传 <input type="file" id="uploadExcel" multiple @change="logFileList" /> <el-button @click="submit">开始添加</el-button>
引入xlsx
import * as XLSX from "xlsx";
下面是具体的实现方法
1 data() { 2 return { 3 resultData: [] // 处理完的数据 4 } 5 } 6 methods: { 7 logFileList(e) { 8 9 const files = e.target.files; 10 const rawFile = files[0]; 11 if (!rawFile) return; 12 return new Promise((resolve, reject) => { 13 const reader = new FileReader(); 14 reader.onload = (e) => { 15 const data = e.target.result; 16 const workbook = XLSX.read(data, { type: "array", cellDates: true }); 17 const firstSheetName = workbook.SheetNames[2]; //表格分页 18 const worksheet = workbook.Sheets[firstSheetName]; 19 const results = XLSX.utils.sheet_to_json(worksheet); 20 // this.generateData({ header, results }) 21 console.log(results, "读取的数据"); 22 const resultData = []; 23 results.forEach((item, index) => { 24 // if(index!=0 || index!=results.length-1){ 25 if (index >0 ) { //去掉表头数据 26 let data = {}; 27 data.deptInputTime = this.parseTime( 28 item[ 29 "郑州市中心医院航空港区方舱医院\n2022年11月医疗废物收运明细" 30 ] 31 ); 32 data.timenumber = Date.parse( 33 item[ 34 "郑州市中心医院航空港区方舱医院\n2022年11月医疗废物收运明细" 35 ] 36 ); 37 data.timesfm = 38 data.timenumber + 39 3600 * 1000 * 15 + 40 this.getRandomInt(0, 3600 * 1000 * 3); //时间范围在 15点-18点 41 data.deptInputTime = this.parseTime(data.timesfm); 42 data.deptId = 9253; 43 data.deptUserName = "董鹏飞"; 44 data.handleId = 8365; 45 data.handleReceiveTime = data.deptInputTime; //时间 46 data.handleReceiveUser = "宁晓刚"; 47 data.logisticsCollectTime = data.deptInputTime; //时间 48 data.logisticsRepeatWeight = item.__EMPTY; 49 data.logisticsUserName = 17827; 50 data.medicalWasteBagCode = item.__EMPTY_5; 51 data.medicalWasteCreateWeight = item.__EMPTY; 52 data.medicalWasteFirstSort = "1"; 53 data.transferBoxCode = "941017392490001"; 54 data.transferUserName = 17827; 55 data.warehousingTime = data.deptInputTime; //时间 56 data.warehousingUserName = 17827; 57 data.warehousingWeight = item.__EMPTY; 58 resultData.push(data); 59 } 60 }); 61 console.log(resultData, "发送的数据"); 62 this.resultData = resultData; 63 64 resolve(); 65 }; 66 reader.readAsArrayBuffer(rawFile); 67 }); 68 }, 69 submit() { 70 if (this.resultData.length != 0) { 71 let data = this.resultData.shift(); 72 addAbnormal(data).then((res) => { 73 if (res.code == 200) { 74 console.log(res, "请求结果"); 75 this.msgSuccess("成功"); 76 console.log(this.resultData, "剩余数据"); 77 } 78 }); 79 } else { 80 this.msgError("没有数据了"); 81 } 82 }, 83 getRandomInt(min, max) { 84 min = Math.ceil(min); 85 max = Math.floor(max); 86 return Math.floor(Math.random() * (max - min) + min); 87 }, 88 89 }
发送数据 由于接口仅支持一条数据添加,所以为了防止循环添加导致接口报错,采用了点击一次添加一套数据的方式。
标签:const,读取,数据,xls,results,item,resultData,录入,data From: https://www.cnblogs.com/esmusssein1/p/17026805.html