xlsx插件处理日期格式,以及某列第一行无数据情况 列不显示问题
1.问题:日期格式会显示数字
解决方法:XLSX.read的配置项里 设置 cellDates: ‘d‘
设置好后会显示标准日期格式,配置项里有 dateNF是可以设置日期格式的,但是我还没弄清,就手动处理了下
看红色字体部分
2.问题:如果第一行数据的某一列是空值,这一列回不显示
解决方法:
在XLSX.utils.sheet_to_json这个方法里添加配置项, 给空值添加一个默认值,如图蓝色字部分
// 通过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 }); for (const i in wb.Sheets[wb.SheetNames[0]]) { if(wb.Sheets[wb.SheetNames[0]][i][‘v‘] !== null && wb.Sheets[wb.SheetNames[0]][i][‘v‘] !== undefined && wb.Sheets[wb.SheetNames[0]][i][‘t‘] == ‘d‘){ wb.Sheets[wb.SheetNames[0]][i][‘v‘] = _this.dateFormat(new Date(wb.Sheets[wb.SheetNames[0]][i][‘w‘]), ‘yyyy/MM/dd‘); } } } outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], { defval: ‘‘ } ); //到这里就是获取到数据了 // 下面时在文件中记录数据 自行修改 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); } })
相关推荐
zrtlin 2020-11-09
xuebingnan 2020-11-05
wikiwater 2020-10-27
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25
Aveiox 2020-06-23
dragonzht 2020-06-17