natergj / excel4node

Node module to allow for easy Excel file creation
MIT License
1.38k stars 215 forks source link

downloading as xlsx gives the corrupted data error #322

Closed AutomataVM closed 4 years ago

AutomataVM commented 4 years ago

When I download the data on the client, I always receive the "this excel is corrupted" message. The filesize is also different and I do not know why.

To Reproduce on the server:

var router = require('express').Router(),
xl          = require('excel4node');

router.post('/createHoursReport',validation.validatePermissionMethod('objectives/update'),
    function(req,res){
// Create a new instance of a Workbook class
                    var wb = new xl.Workbook();

                    // Add Worksheets to the workbook
                    var ws = wb.addWorksheet('reporte de horas');

// Set value of cell A1 to 100 as a number type styled with paramaters of style
                    ws.cell(1, 1)
                    .number(100);

wb.write('reporte.xlsx',res);       
                });

on the client (angular js 1)

$scope.createReport = function(){
            $http.post('/export/createHoursReport?'+ $.param({campaign : campaign_id, date : $('#date').val(), type: 'hours'},{responseType:'blob'}))
            .success(function(data) {
                if (data.status){
                    console.log(data.status);
                }
                else{//se recibio el buffer
                    var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
                    var link = document.createElement('a');
                    link.href = window.URL.createObjectURL(blob);
                    link.download = 'reporte.xlsx';
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                }
            })
        }

Expected behavior I want to download a clean excel file with the contents as generated on the server

Environment (please complete the following information):

giordifungula commented 4 years ago

I am having a similar issue, but I am using node mailer to email the excel sheet that gets generated, its showing that the file is corrupt. So I am also looking around for possible solutions and help.

AutomataVM commented 4 years ago

I managed to solve it by myself, here's what I did

server side: wb.writeToBuffer().then((buffer) => { const binaryBuffer = Buffer.from(buffer); res.attachment('Spreadsheet.xlsx'); return res.status(200).send({status:'ok',file:binaryBuffer}); });

client side:

function toArrayBuffer(buf) { var ab = new ArrayBuffer(buf.length); var view = new Uint8Array(ab); for (var i = 0; i < buf.length; ++i) { view[i] = buf[i]; } return ab; } var file = data.file;

                var blob = new Blob([toArrayBuffer(file.data)],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});

                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = 'reporte.xlsx';
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
giordifungula commented 4 years ago

Thanks @juanpablo64 do we want to close this and mark it resolved :)