使用xlsx库,仅导出Angular 7的角材料6的分页数据表的可见行,而不是整个数组 [英] Angular 7 only visible rows of paginated data table of angular material 6 are exported instead of the whole array using xlsx library

查看:161
本文介绍了使用xlsx库,仅导出Angular 7的角材料6的分页数据表的可见行,而不是整个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 xlsx 库将角形材料数据表导出到excel文件中。



问题就是说,如果我在数据表中使用分页,并且在导出时,仅导出可见行:

  exportTable( )
{
//让data = Object.values(this.dataSource);
const ws:xlsx.WorkSheet = xlsx.utils.table_to_sheet(this.table.nativeElement);
const wb:xlsx.WorkBook = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(wb,ws,所有数据导出);

/ *保存到文件* /
xlsx.writeFile(wb,‘ExportAllData.xlsx’);
}

我尝试使用 json_to_sheet()<导出它/ code>:

  exportTable()
{
let data = this.allData;
const ws:xlsx.WorkSheet = xlsx.utils.json_to_sheet(data);
const wb:xlsx.WorkBook = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(wb,ws,所有数据导出);

/ *保存到文件* /
xlsx.writeFile(wb,‘ExportAllData.xlsx’);
}

但是标头现在是索引而不是字段标题。



这是描述问题的 stackblitz 。 p>

我应该使用第二种方法,是使用 .push()将标题行添加到数组中,然后下载吗?还是使用 ng-table-export 库更好?

解决方案

尝试使用json_to_sheet函数的options参数;

 让options:JSON2SheetOpts = {标头:['Name','Surname', '年龄']}; 
const ws:XLSX.WorkSheet = XLSX.utils.json_to_sheet(data,options);
const wb:XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb,ws,所有数据导出);
XLSX.writeFile(wb,excelFileName +‘.xlsx’);


I need to export an angular material data table into excel file, using xlsx library.

The issue is that, if I am using pagination within the data table, and on export, only the visible rows are exported:

exportTable()
  {
    //let data = Object.values(this.dataSource);
    const ws: xlsx.WorkSheet=xlsx.utils.table_to_sheet(this.table.nativeElement);
    const wb: xlsx.WorkBook = xlsx.utils.book_new();
    xlsx.utils.book_append_sheet(wb, ws, 'All Data Export');

    /* save to file */
    xlsx.writeFile(wb, 'ExportAllData.xlsx');
  }

I tried to export it using json_to_sheet():

  exportTable()
  {
    let data = this.allData;
    const ws: xlsx.WorkSheet=xlsx.utils.json_to_sheet(data);
    const wb: xlsx.WorkBook = xlsx.utils.book_new();
    xlsx.utils.book_append_sheet(wb, ws, 'All Data Export');

    /* save to file */
    xlsx.writeFile(wb, 'ExportAllData.xlsx');
  }

But the header is now indexes instead of field titles.

Here is a stackblitz describing the issue.

Should I use the second method by adding a header row to the array using .push() and then download it ? Or is it better to use ng-table-export library ?

解决方案

Try using options parameter of json_to_sheet function;

let options:JSON2SheetOpts  = {header: ['Name', 'Surname', 'Age']};
const ws: XLSX.WorkSheet=XLSX.utils.json_to_sheet(data, options);
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'All Data Export');
XLSX.writeFile(wb, excelFileName + '.xlsx');

这篇关于使用xlsx库,仅导出Angular 7的角材料6的分页数据表的可见行,而不是整个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆