完整版代码:
处理日期格式的话,要在xlsx.read里面加上 cellDates: true inp.change(function (event) {
// 通过DOM取文件数据 this.file = event.currentTarget.files[0]; var rABS = false; //是否将文件读取为二进制字符串 var f = this.file; var reader = new FileReader(); FileReader.prototype.readAsBinaryString = function(f) { _this.excelDialogInfo.isShowExcelDialog = true; var binary = ""; var rABS = false; //是否将文件读取为二进制字符串 var wb; //读取完成的数据 var outdata; var reader = new FileReader(); reader.onload = function(e) { var bytes = new Uint8Array(reader.result); var length = bytes.byteLength; for(var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); } var XLSX = require('xlsx'); if(rABS) { wb = XLSX.read(btoa(fixdata(binary)), { //手动转化 type: 'base64' }); } else { wb = XLSX.read(binary, { type: 'binary', cellDates: true }); } outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); //到这里就是获取到数据了
// 下面时在文件中记录数据 自行修改 if(outdata && outdata.length > 0){ let columns = []; console.log(outdata[0]); for(let key in outdata[0]){ columns.push({ field: key, }); } _this.excelDialogInfo.columns = columns; _this.excelDialogInfo.data = outdata; }else{ MessageBox.alert('所选表格数据为空,请重新选择', '消息', {type: 'warning'}); }
} reader.readAsArrayBuffer(f); } if(rABS) { reader.readAsArrayBuffer(f); } else { reader.readAsBinaryString(f); } })