我需要使用xlsx
库将角度数据表导出到Excel文件中。
问题是,如果我在数据表中使用分页,并且在导出中,只有可见行被导出:
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');
}
我尝试使用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');
}
,但是标题现在是索引而不是字段标题。
这是一个描述该问题的stackblitz。
我应该通过使用.push()
向数组中添加标头行来使用第二种方法,然后下载它?还是使用ng-table-export
库更好?
尝试使用JSON_TO_SHEET函数的选项参数;
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');
exportexcel(): void {
/* list object is passed over here */
const result = this.companyList.map((x) => ({
// ColumnName: x.data,
COMPANY_ID: x.COMPANY_ID,
COMPANY_EMAIL: x.COMPANY_EMAIL,
COMPANY_NAME: x.COMPANY_NAME,
ADDRESS: x.ADDRESS,
COMPANY_CONTACT: x.COMPANY_CONTACT,
}));
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(result);
/* generate workbook and add the worksheet */
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
/* save to file */
XLSX.writeFile(wb, this.fileName);
}