方法一:
缺点:导出的Excel文件,单元格边框丢失;
解决方法:把数据写入到table里面(为table写边框样式),但是有数据的有边框,没有数据的单元格边框依然缺失;
参考文章:https://blog.csdn.net/fn_2015/article/details/54943868
步骤如下:
let arr = ['方法1'];
let blob = new Blob([arr], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
let objectUrl = URL.createObjectURL(blob);
let a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display:none');
a.setAttribute('href', objectUrl);
a.setAttribute('download', "导出.xlsx");
a.click();
URL.revokeObjectURL(objectUrl);
方法二:
参考文章:https://blog.csdn.net/qq_39252501/article/details/79656317
步骤如下:
安装xlsx和file-saver;
let arr = [{id: '1',name:'22'}];
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(arr);
const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
const data: Blob = new Blob([excelBuffer], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
FileSaver.saveAs(data, "导出.xlsx");