美文网首页
Angularjs 导出数据到Excel

Angularjs 导出数据到Excel

作者: Lay小心心 | 来源:发表于2018-08-07 14:30 被阅读31次

方法一:
缺点:导出的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");

相关文章

网友评论

      本文标题:Angularjs 导出数据到Excel

      本文链接:https://www.haomeiwen.com/subject/ehtavftx.html