首先认识一下CSV
csv---泛指具有以下特征的任何文件
纯文本,使用某个字符集,比如ASCII,Unicode,EBCDIC或GB2312
有不同行的记录组成,
每条记录被分隔符分隔成字段(典型分隔符有逗号,分号或制表符)
每条记录都有同样的字段序列
一句话总结:CSV只是数据用逗号分隔而已的普通TXT文件,实际你用记事本打开,也就是一串逗号分隔的字符
上代码解析
用excel打开的话,处理列名
用excel展示,将数据依次放入对应的列下
以本地方式保存文件
最后,上代码
columns=[];csvSeparator = ',';exportList(){let tmpArr = this.exportData;let csv = '\ufeff';for(let i in this.customList){//不显示操作列// if(this.customList[i].checked && this.customList[i].value != 'item'){if(this.customList[i].checked ){if(this.customList[i].value != 'item'){this.columns.push(this.customList[i].value);const column = this.customList[i].labelcsv += '"' + (column.header || column) + '"';csv += this.csvSeparator;}}}//bodytmpArr.forEach((record) => {let tmp ;csv += '\n';let stateNm = this.checkState('',record.state)record['LoadBalancerClass'] = '公网' ;record['ChargeType'] = '按需付费' ;record['state'] = this.checkState('',record.state)record['ExpireTime'] = '--' ;if(!record['vpc_name']){record['vpc_name'] = '--';}for (let i_1 = 0; i_1 < this.columns.length; i_1++) {const column = this.columns[i_1];if(column == 'business_ip' && record.eip_id !=null){tmp = record.business_ip;record.business_ip = record.business_ip+'(内)' +'\r\n'+ record.eip_address+'(外)'}else if(column == 'business_ip' && record.eip_id ==null){tmp = record.business_ip;record.business_ip = record.business_ip+'(内)' +'\r\n'+ '--(外)'}csv += '"' + this.resolveFieldData(record, column) + '"';column == 'business_ip' ? record.business_ip = tmp : record.business_ip;if (i_1 < (this.columns.length - 1)) {csv += this.csvSeparator;}}});const blob = new Blob([csv], {type: 'text/csv;charset=utf-8;'});let newDate = new Date();let month=newDate.getMonth()+1;let day=newDate.getDate();let yearMonth = newDate.getFullYear()+''+(month<10 ? "0"+month:month)+(day<10 ? "0"+day:day);console.log(yearMonth)if (window.navigator.msSaveOrOpenBlob) {navigator.msSaveOrOpenBlob(blob, "SLB_INFO_"+ yearMonth + '.csv');} else {const link = document.createElement('a');link.style.display = 'none';document.body.appendChild(link);if (link.download !== undefined) {link.setAttribute('href', URL.createObjectURL(blob));link.setAttribute('download', "SLB_INFO_"+ yearMonth + '.csv');link.click();} else {csv = 'data:text/csv;charset=utf-8,' + csv;window.open(encodeURI(csv));}document.body.removeChild(link);}this.refresh();}