导出 Excel

this.dataSource.data 是个数组,每一个元素是 table 里的一行

思路就是:每个表格元素 列 后面加 ',' 每个表格元素 行后面加 '\n' 然后把数据拼接成字符串 targetTable

 1 exportTable() {
 2         let targetTable = '用户昵称,被邀请码,用户电话,实际邀请人数,总邀请人数,查询时的时间\n';
 3         for (let i = 0; i < this.length; i++) {
 4             targetTable += this.dataSource.data[i].nickname + ',';
 5             targetTable += this.dataSource.data[i].from_code + ',';
 6             targetTable += this.dataSource.data[i].phone + ',';
 7             targetTable += this.dataSource.data[i].real_invite_count + ',';
 8             targetTable += this.dataSource.data[i].total_invite_count + ',';
 9             targetTable += this.dataSource.data[i].now_time + '\n';
10         }
11 
12         targetTable = unescape(encodeURIComponent(targetTable));  // support chinese
13         if (this.length > 0) {
14             const a = document.createElement('a');
15             a.href = `data:application/vnd.ms-excel;base64,${btoa(targetTable)}`;
16             a.download = 'inviteRank.csv';
17             a.click();
18         }
19     }

 

 

posted @ 2018-09-18 09:40  海无泪  阅读(102)  评论(0编辑  收藏  举报