js导出csv

export() {
    // 方法一 200K 火狐不支持
    const a = document.createElement('a');
    let href = 'data:text/csv;charset=utf-8,\uFEFF《' + this.name + '》数字电影制作费用清单\n上映日期:' + this.releaseDate + '\n';
    href += '序号,制作内容,单位,单价(元),数量,金额(元),备注\n';
    let i = 1;
    for (const d of this.tableData) {
      href += i + ',' + d.fee_name + ',' + d.unit + ',' + d.price + ',' + d.number + ',' + d.amount + ',' + d.remark + '\n';
      i++;
    }
    href += '合计,,,,,' + this.reportTotalTee + '\n,,,,,,华夏电影发行有限责任公司\n制表日期:' + this.now;
    href = encodeURI(href);
    console.log(href);
    this.href = href;
    console.log(href);
    a.href = 'data:text/csv;charset=utf-8,\uFEFF' + href;
    a.download = this.name + '-费用清单.csv';
    console.log(a.click);
    a.click();

    // 方法二 依赖xlsx 1.5M
    const data = [['《' + this.name + '》数字电影制作费用清单'], ['上映日期:' + this.releaseDate]];
    let i = 1;
    for (const d of this.tableData) {
      data.push([i, d.fee_name, d.unit, d.price, d.number, d.amount, d.remark]);
      i++;
    }
    data.push(['合计', , , , , this.reportTotalTee]);
    data.push([, , , , , , '华夏电影发行有限责任公司']);
    data.push([, , , , , , '制表日期:' + this.now]);
    const ws: XLSX.WorkSheet = XLSX.utils.aoa_to_sheet(data);

    const wb: XLSX.WorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

    XLSX.writeFile(wb, '《' + this.name + '》费用合计.xlsx');

    // 方法三 可以设置style, 依赖 xlsx-style xlsx.core.min.js
    const data = [
      {
        id: '《' + this.name + '》数字电影制作费用清单',
        fee_name: '',
        unit: '',
        price: '',
        number: '',
        amount: '',
        remark: ''
      },
      {
        id: '上映日期:' + this.releaseDate,
        fee_name: '',
        unit: '',
        price: '',
        number: '',
        amount: '',
        remark: ''
      },
      {
        id: '序号',
        fee_name: '制作内容',
        unit: '单位',
        price: '单价(元)',
        number: '数量',
        amount: '金额(元)',
        remark: '备注'
      }
    ];
    for (let index = 0; index < this.tableData.length; index++) {
      const c = this.tableData[index];
      data.push({
        id: (index + 1) + '',
        fee_name: c.fee_name,
        unit: c.unit,
        price: c.price,
        number: c.number,
        amount: c.amount,
        remark: c.remark
      });
    }
    data.push({
      id: '合计',
      fee_name: '',
      unit: '',
      price: '',
      number: '',
      amount: this.reportTotalTee,
      remark: ''
    },
    {
      id: '',
      fee_name: '',
      unit: '',
      price: '',
      number: '',
      amount: '',
      remark: '华夏电影发行有限责任公司'
    },
    {
      id: '',
      fee_name: '',
      unit: '',
      price: '',
      number: '',
      amount: '',
      remark: '制表日期:' + this.now
    });
    console.log(data);
    const keyMap = [];
    for (const k of Object.keys(data[0])) {
      keyMap.push(k);
    }
    console.log(keyMap);
    const tmpdata = []; // 转换好的data
    data.map((v, i) => keyMap.map((k, j) => Object.assign({}, {
      v: v[k],
      position: (j > 25 ? this.getCharCol(j) : String.fromCharCode(65 + j)) + ((i + 1) + '')
    }))).reduce((prev, next) => prev.concat(next)).forEach((v, i) => tmpdata[v.position] = {
      v: v.v
    });
    const outputPos = Object.keys(tmpdata); // 填充区域
    tmpdata['A1'].s = {
      font: {sz: 16, bold: true, align: 'center'},
      alignment: {vertical: 'center', horizontal: 'center'}
    };
    const style_title = {
      font: {sz: 12, bold: true, align: 'center'},
      alignment: {vertical: 'center', horizontal: 'center'},
      border: {top: {style: 'thin'}, bottom: {style: 'thin'}, left: {style: 'thin'}, right: {style: 'thin'}}
    };
    const style_content = {
      font: {sz: 12, align: 'center'},
      alignment: {vertical: 'center', horizontal: 'center'},
      border: {top: {style: 'thin'}, bottom: {style: 'thin'}, left: {style: 'thin'}, right: {style: 'thin'}}
    };
    let i = 0;
    for (const t of Object.keys(data[0])) {
      tmpdata[String.fromCharCode(65 + i) + 3].s = style_title;
      tmpdata[String.fromCharCode(65 + i) + (this.tableData.length + 4)].s = style_content; // 合计部分
      i++;
    }

    tmpdata['!merges'] = [
      {
        s: {c: 0, r: 0},
        e: {c: 6, r: 0}
      },
      {
        s: {c: 0, r: 1},
        e: {c: 6, r: 1}
      },
      {
        s: {c: 0, r: this.tableData.length + 3},
        e: {c: 4, r: this.tableData.length + 3}
      }
    ]; // 合并单元格
    const tmpWB = {
      SheetNames: ['mySheet'], // 保存的表标题
      Sheets: {
          'mySheet': Object.assign({},
              tmpdata, // 内容
              {
                  '!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1] // 设置填充区域
              })
      }
    };
    const tmpDown = new Blob([this.s2ab(XLSX.write(tmpWB,
      {bookType: 'xlsx', bookSST: false, type: 'binary'} // 这里的数据是用来定义导出的格式类型
    ))], {
        type: ''
    });

    const tmpa = document.createElement('a');
    document.body.appendChild(tmpa);
    tmpa.style.display = 'none';
    tmpa.download = this.name + '_费用清单.xlsx';
    tmpa.href = URL.createObjectURL(tmpDown);
    tmpa.click();
    // setTimeout(function () {
        // URL.revokeObjectURL(tmpDown); // 清除
    // }, 100);
  }
  getCharCol(n) {
    let s = '', m = 0;
    while (n > 0) {
      m = n % 26 + 1;
      s = String.fromCharCode(m + 64) + s;
      n = (n - m) / 26;
    }
    return s;
  }
  s2ab(s) {
    if (typeof ArrayBuffer !== 'undefined') {
        const buf = new ArrayBuffer(s.length);
        const view = new Uint8Array(buf);
        for (let i = 0; i !== s.length; ++i) {view[i] = s.charCodeAt(i) & 0xFF; }
        return buf;
    } else {
      const buf = new Array(s.length);
        for (let i = 0; i !== s.length; ++i) {buf[i] = s.charCodeAt(i) & 0xFF; }
        return buf;
    }
  }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值