npm install xlsx --save

chooseFile(){
this.excelData = [];
document.getElementById(this.fileId).value = null;
this.imFile.click();
},
importf(obj) {
let _this = this;
this.file = this.$refs.imFile.files[0];
// this.$refs.imFile.value = null;
var rABS = false; //是否将文件读取为二进制字符串
var file = this.file;

FileReader.prototype.readAsBinaryString = function(f) {
var binary = "";
var rABS = false; //是否将文件读取为二进制字符串
var pt = this;
var workbook; //读取完成的数据
// var excelData;
var reader = new FileReader();
reader.onprogress = function(e) {
let total = file.size;
_this.progress = (e.loaded/total)*100;
console.log( _this.progress);
};
reader.onload = function(e) {
try {
var bytes = new Uint8Array(reader.result);
var length = bytes.byteLength;
for(var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
if(rABS) {
workbook = XLSX.read(btoa(fixdata(binary)), { //手动转化
type: 'base64'
});
}else {
workbook = XLSX.read(binary, {
type: 'binary'
});
}
// excelData = [];
} catch(e) {
layer.toptip("文件类型不正确")
return;
}
var fromTo = '';
for (var sheet in workbook.Sheets) {
if (workbook.Sheets.hasOwnProperty(sheet)) {
fromTo = workbook.Sheets[sheet]['!ref'];
_this.excelData = _this.excelData.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
}
}
_this.$emit("fileUpload",_this.excelData)
};

reader.readAsArrayBuffer(f);

}

var reader = new FileReader();
if(rABS) {
reader.readAsArrayBuffer(file);
}else {
reader.readAsBinaryString(file);
}

}

参考网址:https://www.jianshu.com/p/74d405940305

posted on 2020-07-20 17:29  去你的未来  阅读(782)  评论(0编辑  收藏  举报