如何不借助插件前端自己写导出excel --csv格式

exportCsv(title, data, fileName){
        let today = new Date();
        let tadayDate = today.getFullYear() + "-" +(today.getMonth() + 1) + "-" + today.getDate();
        fileName = "学校报名列表-" + tadayDate;
        var CSV = '';
        var row = "";
 
        let temp = []
        data=this.appTable
 
        title=[
        {title:'名字1',field:'account_name'},
        {title:'名字2',field:'student_name'},
        {title:'名字3',field:'application_project'},
        {title:'子名字4',field:'application_events'},
        {title:'名字',field:'application_channel'},
        ]
        
        for (var i = 0; i < title.length; i++) {
          if(title[i].title){
            row += title[i].title + ',';
          }
        }
        row = row.slice(0, -1);
        CSV += row + '\r\n';
        
        for (var i = 0; i < data.length; i++) {
          var row = "";
          for (var j = 0; j < title.length; j++) {
          if(title[j].title){        
             row += '"' + data[i][title[j].field] +'"\t,';
          }
          }
          row.slice(0, row.length - 1);
          CSV += row + '\r\n';
        }
        
        if (CSV == '') {
          alert("Invalid data");
          return;
        }
        
        var fileName = fileName;
        var uri = new Blob(['\ufeff' + CSV], {type:"text/csv"});
        
        if (window.navigator && window.navigator.msSaveOrOpenBlob) { // for IE
          window.navigator.msSaveOrOpenBlob(CSV, fileName + ".csv");
        } else { // for Non-IE(chrome、firefox etc.)
          var link = document.createElement("a");
          link.href = URL.createObjectURL(uri);
        
          link.style = "visibility:hidden";
          link.download = fileName + ".csv";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        }
        },

posted @ 2020-12-01 17:15  gunelark  阅读(183)  评论(0编辑  收藏  举报