angular2导出到excel

问题描述 投票:0回答:2

我尝试导出excel在角度,但我得到错误为“无法读取属性'useBom'未定义。请帮我解决此错误”

let options = {
      fieldSeparator: ',',
      quoteStrings: '"',
      decimalseparator: '.',
      showLabels: false,
      headers: [],
      showTitle: true,
      title: 'asfasf',
      useBom: true,
      removeNewLines: true,
      keys: ['approved','age','name' ]
    };
   let data = [
      {
        name: "Test, 1",
        age: 13,
        average: 8.2,
        approved: true,
        description: "using 'Content here, content here' "
      }
    ];
  }
<angular2csv [data]="data" filename="test.csv" [options]="options" ></angular2csv>`enter code here`
angular angular2-template
2个回答
0
投票

看起来你的dataoptions不是类字段,而是方法中的一些局部变量(我猜)。看着the example。您应该将dataoptions作为组件类的字段,以便模板可以“看到”它们。


0
投票

首先,我们使用papaparse ... enter link description here

import { Papa } from 'ngx-papaparse';
import { exportCsv } from 'app/modules/core/functions/export-csv.function';

exportSummaryCsv() {
    const title = `Story Funnel Summary`;
    const summary = this._columns.map(column => ({
        'Stage': column.name,
        'Quantity': column.items.length
    }));
    const csv = this.csvParser.unparse(summary);
    exportCsv(title, csv);
}

然后我们创建一个blob和一个链接,并在DOM中插入一个链接

export function exportCsv(filename: string, csv: string) {
    filename = `${filename.replace(/ /g, '_')}.csv`;

    const blob = new Blob([csv], {'type': 'text/csv;charset=utf8;'});
    const link = document.createElement('a');

    link.download = filename;
    link.href = URL.createObjectURL(blob);
    link.setAttribute('visibility', 'hidden');

    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

然后是一个触发所有这一切的按钮......

© www.soinside.com 2019 - 2024. All rights reserved.