本地将数据导出到本地CSV文件

export = () => {
    const data = {
      trade: {
        filterVal: [],
        list: [],
      },
    };
    const nodes = [
      {
        entityName: '真实身份',
        entityNum: '010100',
        id: '010100_0010111_142702199711111111',
        label: '142702199711111111\n李晓慧\n汉族',
        pageJumpParam: {
          encryptParam: 'c6f995bed195c881',
          entityType: '0010111',
          type: 'person',
          notEncryptParam: '142702199711111111',
        },
        showEntityProperties: [
          { en: 'name', cn: '姓名', value: '李晓' },
          { en: 'nation', cn: '民族', value: '汉族' },
          { en: 'entity_value', cn: '证件号码', value: '142702199711111111' },
        ],
      },
      {
        entityName: '虚拟身份',
        entityNum: '010000',
        id: '010000_0010100_142702199711111111',
        label: '142702199711111111\n李晓\n汉族',
        pageJumpParam: {
          encryptParam: 'c6f995bed195c881',
          entityType: '0010111',
          type: 'person',
          notEncryptParam: '142702199711111111',
        },
        showEntityProperties: [
          { en: 'name', cn: '姓名', value: '李晓' },
          { en: 'nation', cn: '民族', value: '汉族' },
          { en: 'entity_value', cn: '证件号码', value: '142702199711111111' },
        ],
      },
    ];
    data.trade.list = props.data.nodes.map((item) => {
        return {
          ...item,
          label: item.label?.split('\n').join('-'),
        };
      });
     //  注:csv文件:","逗号换列,\n换行,\t防止excel将长数字变科学计算法等样式
    const mainLists = data.trade;
    const mainTitleForKey = mainLists.filterVal; //一级过滤
    const mainList = mainLists.list; //一级数据
    for (const th in mainList[0]) {
      mainTitleForKey.push(th);
    }
    const mainStr = [];
    mainStr.push(mainTitleForKey.join('\t,') + '\n'); //标题添加上换列转成字符串并存进数组
    for (let i = 0; i < mainList.length; i++) {
      const temp = [];
      for (let j = 0; j < mainTitleForKey.length; j++) {
        //根据过滤器拿出对应的值
        temp.push(
          typeof mainList[i][mainTitleForKey[j]] === 'undefined'
            ? mainList[i][mainTitleForKey[j]]
            : JSON.stringify(mainList[i][mainTitleForKey[j]]).split(',').join(','),
        );
      }
      mainStr.push(temp.join('\t,') + '\n'); //取出来的值加上逗号换列转字符串存数组
    }
    const merged = mainStr.join('');
    //## 导出操作
    // encodeURIComponent解决中文乱码
    const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(merged);
    // 通过创建a标签实现
    const link = document.createElement('a');
    link.href = uri;
    // 对下载的文件命名
    link.download = new Date().toISOString().substring(0, 10) + '-' + type + '.csv';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文哈哈wcx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值