1

I wanna generate the excel from laravel with PHPspreadsheet (PHP Lib) and then return the xlsx file to the frontend to trigger the download function. is that possible?

JSX Part

axios
.get(
    "/excel/export/dashboardTable", {}
)
.then(resp => {
    //success callback
    if (resp.status == 200) {
        const blob = new Blob([resp.data], { type: "application/vnd.ms-excel" });
        let link = URL.createObjectURL(blob);
        let a = document.createElement("a");
        a.download = "Customers.xlsx";
        a.href = link;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }
})
.catch(error => {
    _.forEach(error.response.data.errors, function (
        value,
        el
    ) {
        toastr.error("Error", value, {
            onHidden: function onHidden() { }
        });
    });
})
.finally(() => { });

Backend Part

$response = response()->streamDownload(function () use ($spreadsheet) {
    $writer = new xlsx($spreadsheet);
    $writer->save('php://output');
}, "Dashboard.xlsx");

$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/vnd.ms-excel');

return $response;
5
  • I thought you were talking about laravel-excel package. It is created exactly for your task. Look at how easy it is to export your excel file docs.laravel-excel.com/3.1/exports Commented Feb 11, 2021 at 7:06
  • @AnuratChapanond how about streamDownload function? There's no way to do it? Commented Feb 11, 2021 at 7:09
  • If I understand correctly, streamdownload means you create a spreadsheet without writing to a disk before sending, then yes laravel-excel can do that without having to call streamdownload(). Commented Feb 11, 2021 at 7:12
  • Got it, but is that possible to download the via js not using PHP or link? Commented Feb 11, 2021 at 7:14
  • you can just have your js call the url for the excel file. e.g. location.href = /url/ or something similar. Commented Feb 11, 2021 at 7:16

1 Answer 1

2

Problem Solve: change the responseType to Blob.

axios
.get(
    "/excel/export/dashboardTable", {
    responseType: 'blob' //Change the responseType to blob
}
)
.then(resp => {
    //success callback
    if (resp.status == 200) {
        toastr.success("Success", "Updated successfully!", {
            onHidden: function onHidden() {
                let blob = new Blob([resp.data], { type: "application/vnd.ms-excel" });
                let link = URL.createObjectURL(blob);
                let a = document.createElement("a");
                a.download = "file.xlsx";
                a.href = link;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            }
        });
    }
})
.catch(error => {
    _.forEach(error.response.data.errors, function (
        value,
        el
    ) {
        toastr.error("Error", value, {
            onHidden: function onHidden() { }
        });
    });
})
.finally(() => { });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.