【问题标题】:angular2 export to excelangular2导出到excel
【发布时间】:2019-02-23 10:41:46
【问题描述】:

我正在尝试以角度导出到 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`

【问题讨论】:

  • optionsdata 定义在哪里。它们是组件类的字段吗?
  • 您能否编辑您的问题并显示您的组件(.ts 文件)的外观(包含<angular2csv> 的模板)?

标签: angular angular2-template


【解决方案1】:

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

【讨论】:

  • 这只是我尝试过并且收到类似“无法读取未定义的属性'useBom'”的错误
【解决方案2】:

首先,我们使用 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);
}

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

【讨论】:

  • 感谢您的回复。如何在 angular2 中将 CSS 属性添加到导出到 excel 中
  • 这不仅仅是添加一个 CSS 属性。
  • 是的,但我的要求是为标题添加颜色。那么是否有任何财产可以做到这一点。或者帮我在这个里面添加css。
猜你喜欢
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多