import { Component } from
'@angular/core'
;
import { STColumn, XlsxService } from
'@delon/abc'
;
@Component({
selector:
'components-xlsx-export'
,
template: `
<button nz-button (click)=
"download()"
>Export</button>
<!-- 为了方便使用了ng-alain表格组件,也可以用ng-zorro或者其他库实现的表格。展现是次要的,重点是数据 -->
<st [data]=
"users"
[ps]=
"3"
[columns]=
"columns"
class
=
"mt-sm"
></st>
`,
})
export
class
ComponentsXlsxExportComponent {
constructor(
private
xlsx: XlsxService) {}
users: any[] = Array(100)
.fill({})
.map((_item: any, idx: number) => {
return
{
id: idx + 1,
name: `name ${idx + 1}`,
age: Math.
ceil
(Math.random() * 10) + 20,
};
});
columns: STColumn[] = [
{ title:
'编号'
, index:
'id'
, type:
'checkbox'
},
{ title:
'姓名'
, index:
'name'
},
{ title:
'年龄'
, index:
'age'
},
];
download() {
const
data = [this.columns.map(i => i.title)];
this.users.forEach(i =>
data.push(this.columns.map(c => i[c.index
as
string])),
);
this.xlsx.export({
filename:
'自定义命名列表.xlsx'
,
sheets: [
{
data: data,
name:
'sheet name'
,
},
],
});
}
}