码迷,mamicode.com
首页 > 其他好文 > 详细

浅析CSV----内有代码,,去掉定制化的内容,可直接使用

时间:2019-01-08 21:15:26      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:define   add   付费   div   spl   使用   组成   click   不显示   

首先认识一下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].label
          csv += ‘"‘ + (column.header || column) + ‘"‘;
          csv += this.csvSeparator;
        }
      }
    }
    //body
    tmpArr.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();
  }

 

浅析CSV----内有代码,,去掉定制化的内容,可直接使用

标签:define   add   付费   div   spl   使用   组成   click   不显示   

原文地址:https://www.cnblogs.com/donglt-5211/p/10240975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!