import * as FileSaver from 'file-saver'; //浏览器读取本地的文件已经保存文件所需要的依赖。
.........
let obj = {
exportItem: this.tableList,//[]
exportHeader: headerArr.join(),// ''
exportColumns: keyArr,// []
exportName: fileName || '' //''
}
//导出列表功能
exportList(obj) {
var exportHeader = obj.exportHeader + '\n';
const exportItem = obj.exportItem; // this.item是一个数组,包含需要导出的内容
var exportColumns = obj.exportColumns; //[] 数组每一项是导出的key值
var exportPrefix = '\uFEFF';
var exportContent = exportPrefix + exportHeader;
for (var i in exportItem) {
for (var c in exportColumns) {
exportContent += '' + (exportItem[i][exportColumns[c]] !== null ? exportItem[i][exportColumns[c]] : '') + ',';
}
exportContent += '\n';
}
this.saveAsExcelFile(exportContent, obj.exportName);
}
saveAsExcelFile(buffer: any, fileName: string) {
const data: Blob = new Blob([buffer], {
type: "text/plain;charset=utf-8"
// type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
});
FileSaver.saveAs(data, fileName + '_' + new Date().getTime() + '.csv');
}
网友评论