functionscope / Node-Excel-Export

A simple node.js module for exporting data set to Excel xlsx file.
492 stars 271 forks source link

How client download excel file implement #20

Open nvcken opened 10 years ago

nvcken commented 10 years ago

My server code var file = fs.readFileSync('./res_report/output/out.xlsx', 'binary'); res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); res.setHeader('Content-Disposition', "attachment; filename=" + "out.xlsx") res.end(file, 'binary'); How client javascript download excel file

functionscope commented 10 years ago

try

window.open(replaceWithYourUrl);

On Mon, Sep 29, 2014 at 4:49 AM, nvcken notifications@github.com wrote:

My server code var file = fs.readFileSync('./res_report/output/out.xlsx', 'binary'); res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); res.setHeader('Content-Disposition', "attachment; filename=" + "out.xlsx") res.end(file, 'binary'); How client javascript download excel file

— Reply to this email directly or view it on GitHub https://github.com/functionscope/Node-Excel-Export/issues/20.

nvcken commented 10 years ago

@functionscope var file = fs.readFileSync('./res_report/output/out.xlsx', 'binary'); res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); res.setHeader('Content-Disposition', "attachment; filename=" + "out.xlsx") res.end(file, 'binary'); client I use POST method request to server and server response by above server code My client code angularjs $http.post('/abc/excel', post).success(function(data){ var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); var objectUrl = URL.createObjectURL(blob); window.open(objectUrl); }) .error(function( data){ })

but not work, file open corrupt. What I am wrong

nvcken commented 10 years ago

If I do the same with csv file it work

functionscope commented 10 years ago

check out this link

http://stackoverflow.com/questions/22447952/angularjs-http-post-convert-binary-to-excel-file-and-download

On Sep 29, 2014 7:57 AM, "nvcken" notifications@github.com wrote:

If I do the same with csv file it work

— Reply to this email directly or view it on GitHub.

rajan-g commented 8 years ago

Hi, Me also meet this issue after spent long time I found solution, It is working for me. setup's
1.convert your file as base64 string and send as response EXAMPLE: var result = nodeExcel.execute(conf); return res.status(200).send(new Buffer(result.toString(), 'binary').toString('base64'));
2.client side decodebase64 string, make as blob and create url from blob EXAMPLE: var contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; var sliceSize = sliceSize || 512;

                var byteCharacters = atob(data);
                var byteArrays = [];

                for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
                  var slice = byteCharacters.slice(offset, offset + sliceSize);

                  var byteNumbers = new Array(slice.length);
                  for (var i = 0; i < slice.length; i++) {
                    byteNumbers[i] = slice.charCodeAt(i);
                  }
                  var byteArray = new Uint8Array(byteNumbers);

                  byteArrays.push(byteArray);
                }
                var blob = new Blob(byteArrays, {type: contentType});
                var blobUrl = URL.createObjectURL(blob);
                window.location = blobUrl;

Enjoy