js 下载csv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   exportToExcel() {
            var fileName = "最新质量等级成本";
          const csv = this.convertToCSV(this.datas); // 将数据转换为CSV格式
          const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); // 创建Blob对象
          const url = URL.createObjectURL(blob); // 转换为URL
          const link = document.createElement('a'); // 创建<a>元素
          link.setAttribute('href', url);
          link.setAttribute('download', `${fileName}.csv`);
          link.style.visibility = 'hidden';
          document.body.appendChild(link);
          link.click(); // 触发下载
          document.body.removeChild(link); // 清理
        }
 
    convertToCSV(data) {
      const rows = [];
      console.log('data');
      console.log(data);
      for (let i = 0; i < data.length; i++) {
        const row = [];
 
        for (const [key, value] of Object.entries(data[i])) {
//                let tmp = this.str2utf8(value);
              row.push(`"${value}"`);
            }
        rows.push(row.join(',')); // 将每行数据连接为一个字符串
      }
      return rows.join('\n'); // 将所有行数据连接为一个字符串
    }

  

posted @   CrossPython  阅读(145)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
历史上的今天:
2018-10-13 Cannot assign “A1”: “B1” must be a “C1” instance.
2018-10-13 Django form choices, placeholder
点击右上角即可分享
微信分享提示