Email xlsx file



  • I'm trying to send an xlsx file after its been rendered but I'm receiving it with just </body></html> in one cell. I can send a PDF no problem .

    This is my most recent code. I'm using sendGrid to send the email. I'm assuming it is something to do with the encoding of the file. I've tried with and without the mime type.

    function afterRender(req, res, done) {
    let msg = {
    to: [sendto],
    from: "notifications@test.com",
    subject: "Report",
    html: '<p> Please find report attached </p>',
    attachments: [{
    content: new Buffer(res.content).toString('base64'),
    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    filename: "data.xlsx",
    disposition: "attachment" }]
    }
    mail.send(msg)
    }


  • administrators

    hi! does it work if you change content: new Buffer(res.content).toString('base64') with just res.content.toString('base64')?



  • I get the exact same thing without the Buffer



  • Could you skip the email script and download the xlsx right from the studio? Does it open correctly? Can you attach the xlsx output so we can try to replicate the issue?



  • Yes I can Download the xlsx file just fine and it opens fine. I can download and attach it to an email. Do you want the correct one or the one that only has the </body></html> ?



  • Maybe both... And one more thing to try. If you write the buffer to the file right in the afterRender script, does it write correct excel? This would mean that the problem is in sendgrid somewhere.

    require('fs').writeFileSync('someabsolutpath', res.content)
    


  • No it does not write to the file correctly it seems to give me the same thing. How can i share the xlsx files?



  • So I saved it to a text file to see what I would get:
    <html><head><title>PECO COC Reports All IN</title><body><iframe style="height:100%;width:100%" src="https://view.officeapps.live.com/op/view.aspx?....." /></body></html>



  • ok, I see now. The issue is that if you run the report from the studio, it returns this iframe for direct preview. This would not happen with API call.

    One way to disable excel studio preview from custom script

    function beforeRender(req, res, done) {
        req.options.preview = false
        done()
    }
    

    The second option is to disable it completely in config. This is in docs https://jsreport.net/learn/xlsx#preview-in-studio

    "xlsx": {
      "previewInExcelOnline": false
    }
    


  • That Works Perfectly Thanks so much.
    Question: So I noticed that URL that comes with the studio preview allowed me to access the report without JSReport or any login. When it is generated this way it doesn't create an officeapps.live copy of the document correct?



  • Yes. This way it creates just your local xlsx file.
    We warn every user with dialog at the first render in studio about this behavior.



  • Great, Thanks again.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.