简体   繁体   中英

ag-grid export excel api getDataAsExcel

I am using ag-grid in angular 6 version.

I have provided export button to user for exporting ag-grid data into excel. So, on click event of export I am having following code.

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);
}

This is giving me following error.

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)

what is the issue?

I ran into this issue as well when upgrading to version 17. I was able to solve it while still allowing exporting to xlsx by setting suppressTextAsCDATA in the params to true.

Example:

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);

I removed below lines ....

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);

and kept only ...

this.gridOptions.api.exportDataAsExcel(params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM