js 前端获取excel文件,生成数据传入后端


需求:需要用户点击上传excel文件,获取excel数据,进行值的判断,符合条件通过,不符合过滤

1、使用input file 文件类型的change 事件

<input type="file" ref="upload" accept=".xls,.xlsx,.csv" id="uploadInput" @change="readExcel($event)"/>

2、点击事件内容

readExcel (e) {
var self = this;
const files = e.target.files;
console.log(files);
if (files.length <= 0) {
this.$toast('请输入文件名');
return false;
} else if (!/\.(xls|xlsx|.csv)$/.test(files[0].name.toLowerCase())) {
this.$toast('文件上传失败,请检查你的文件格式');
return false;
} else if (files.size >= 3145728) {
this.$toast('文件上传失败,大小不能超过3M');
return false;
}
const fileReader = new FileReader();
fileReader.onload = (ev) => {
try {
const data = ev.target.result;
const workbook = XLSX.read(data, {
type: 'binary'
});
const wsname = workbook.SheetNames[0];// 取第一张表
const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]);// 生成json表格内容
console.log(ws);
};
// 文件能够传多次
this.$refs.upload.value = '';
fileReader.readAsBinaryString(files[0]);
}
3、数组去重
fliterArry (outputs) {
let hash = {};
let key = '姓名';
let keyCard = '证件号码';
let keymobile = '手机号码';
let filiter = outputs.reduce((total, currentValue) => {
if (currentValue[key] === '99') {
if (!hash[currentValue[keymobile]]) {
hash[currentValue[keymobile]] = true;
total.push(currentValue);
}
} else {
if (!hash[currentValue[keyCard]]) {
hash[currentValue[keyCard]] = true;
total.push(currentValue);
}
}
return total;
}, []);
return filiter;
},


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM