写项目时,遇见了导入导出功能,感觉xlsx插件最方便,记录一下(我使用的是Zorro组件库)
下载插件
cnpm install xlsx --save
执行命令后,确定是否下载成功(我用的版本是0.12.8,其他版本也行)
在组件里使用
新建组件
ng g c components more
把插件引入到组件
在more.component.ts文件里引入
import * as XLSX from 'xlsx';
导出功能
more.components.html文件
<button nz-button nzType="primary" style="float: right; margin-right: 10px;" (click)="daochu()" title="导出到表格">导出</button>
more.components.ts文件
// 定义变量
exportList: any = [];
inputdata = [];
……
ngOnInit() {
this.fun();
}
// 在拿到到接口返回的数据时时,整理出要导出的文件,如这种格式[[1,2,3],[4,5,6]]
fun() {
// res.items就是接口返回的数据
// exportList就是你要导出的数据集合
this.exportList =