0


前端调用后端接口,导出excel文件打开显示[object object]

没有错误提示,输出也正常

一直是这样的,我也找了网上的十几个相同案例,但是都不能解决这个问题

后端传的代码:

let workbook = new exceljs.Workbook();//创建工作簿
let worksheet = workbook.addWorksheet(standarsData[0]["_65766e62408d9400aff73334"]);
const now = Date.now()
    const name = `sf_${now}.xlsx`
    const buffer = await workbook.xlsx.writeBuffer()

    $model.command("tips", {})//隐藏功能提交提示
    $model.result({
        code: "0",
        msg: "ok",
        data: {
            buffer,
            name
        }
    })

未解决前的前端代码:

    let name=res.data.name;
    let buffer=res.data.buffer;
    
    const blob = new Blob([buffer], { type: 'application/octet-stream' });//这个type有很多版本,我都用过也没改成功
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = name; 
    a.click();

解决后端前端代码:

    let name=res.data.name;
    const array = res.data.buffer.data;
    //获取到buffer内部的data
    const buffer = new ArrayBuffer(array.length);
    const view = new Uint8Array(buffer);
    for (let i = 0; i < array.length; i++) {
        view[i] = array[i];
    }
    //主要部分是上面转化数组的部分
    const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    const a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = name; 
    a.click();
解决后:

标签: 前端 excel java

本文转载自: https://blog.csdn.net/qq_45966200/article/details/135091199
版权归原作者 qq_45966200 所有, 如有侵权,请联系我们删除。

“前端调用后端接口,导出excel文件打开显示[object object]”的评论:

还没有评论