ag-grid导出excel api getDataAsExcel

时间:2018-07-13 05:32:28

标签: excel angular export-to-excel ag-grid

我正在使用angular 6版本的ag-grid。

我为用户提供了用于将农业网格数据导出到Excel的导出按钮。因此,在导出的点击事件中,我有以下代码。

onExport() {
  var params = {
            fileName: 'Users',
            allColumns: true
        };
    var content = this.gridOptions.api.getDataAsExcel(params);
    var workbook = XLSX.read(content, {type: 'binary'});
    var xlsxContent = XLSX.write(workbook, {bookType: 'xlsx', type: 'base64'});
    this.myService.download(params, xlsxContent);
}

这给了我以下错误。

UserrolesComponent.html:4 ERROR Error: Unrecognized tag: ![CDATA[URId]]|Workbook,false|Worksheet,false|Table,false
at viewWrappedDebugError (core.js:8439)
at callWithDebugContext (core.js:12214)
at Object.debugHandleEvent [as handleEvent] (core.js:11907)
at dispatchEvent (core.js:8561)
at core.js:9005
at HTMLButtonElement.<anonymous> (platform-browser.js:1215)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4053)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)

问题是什么?

2 个答案:

答案 0 :(得分:2)

在升级到版本17时,我也遇到了这个问题。我能够解决此问题,同时仍然可以通过将参数中的suppressTextAsCDATA设置为true来导出到xlsx。

示例:

var params = {
    suppressTextAsCDATA: true
    // ...
};

var content = this.gridOptions.api.getDataAsExcel(params);
var workbook = XLSX.read(content, {type: 'binary'});
var xlsxContent = XLSX.write(workbook, {bookType: 'xlsx', type: 'base64'});
this.myService.download(params, xlsxContent);

答案 1 :(得分:0)

我删除了下面的行...。

var content = this.gridOptions.api.getDataAsExcel(params);
var workbook = XLSX.read(content, {type: 'binary'});
var xlsxContent = XLSX.write(workbook, {bookType: 'xlsx', type: 'base64'});
this.myService.download(params, xlsxContent);

并且仅保留...

this.gridOptions.api.exportDataAsExcel(params);