Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel file Download Not working in node.js using exceljs

Hi i am new to MEAN Stack.

I want to download the excel file when i click the export button.

I am using this reference link to download the excel file :https://www.npmjs.com/package/exceljs

Html Page

<button ng-click="exportData()" class="btn btn-sm btn-primary btn-create">Export</button>

my controller

var app = angular.module("app", ["xeditable", "angularUtils.directives.dirPagination", "ngNotify", "ngCookies", "ngRoute"]);
        app.config(['$routeProvider', '$httpProvider', function ($routeProvider, $httpProvider) {
        }]);

app.controller('ManageMaterialFlowController', ['$http', '$scope', '$window', '$filter', '$notify', '$cookieStore',  'StoreService',
 function ($http, $scope, $window, $filter, $notify, $cookieStore, StoreService, $routeProvider) {



     //download excel file button click

     $scope.exportData = function () {

         router.get('/download', function (req, res) {

             try {
                 var Excel = require('exceljs');
                 var workbook = new Excel.Workbook();
                 var options = {
                     filename: './Excel.xlsx',
                     useStyles: true,
                     useSharedStrings: true
                 };
                 var workbook = new Excel.Workbook();
                 var worksheet = workbook.addWorksheet('My Sheet');

                 worksheet.columns = [
                     { header: 'Id', key: 'id', width: 10 },
                     { header: 'Name', key: 'name', width: 32 },
                     { header: 'D.O.B.', key: 'DOB', width: 10 }
                 ];
                 worksheet.addRow({ id: 1, name: 'John Doe', dob: new Date(1970, 1, 1) });
                 worksheet.addRow({ id: 2, name: 'Jane Doe', dob: new Date(1965, 1, 7) });

                 var tempFilePath = tempfile('.xlsx');
                 workbook.xlsx.writeFile(tempFilePath).then(function () {
                     console.log('file is written');
                     res.sendFile(tempFilePath, function (err) {
                         console.log('---------- error downloading file: ' + err);
                     });
                 });
             } catch (err) {
                 console.log('OOOOOOO this is the error: ' + err);
             }

         });

     };
}

I don't know how to do this. is this is correct to way to download the excel file by clicking the button.

when I click the button i getting router is not defined error. Can any one solve my issue.

like image 333
Vinoth Avatar asked Oct 16 '25 17:10

Vinoth


2 Answers

Do not use

workbook.xlsx.writeFile()

writeFile() method is for saving file to hard disk.

Instead, use write() method. This method writes file to a stream.

res object is a Writable stream. So you can use like this.

workbook.xlsx.write(res)

and you don't need to call

res.sendFile(tempFilePath) 

Because you already piplined excel file to res object. So code is like this

workbook.xlsx.write(res).then(function () {
    res.status(200).end();
});

Finally, You should add https headers on res object.

res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

res.setHeader("Content-Disposition", "attachment; filename=YOUR_FILENAME.xlsx");

Content-Type notify Web Browser that what data type is.

Content-Disposition notify Web Browser that this data will be saved to hard disk.

Final code is here.


res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

res.setHeader("Content-Disposition", "attachment; filename=Rep1ort.xlsx");

workbook.xlsx.write(res).then(function () {
    res.status(200).end();
});
like image 53
LKB Avatar answered Oct 19 '25 07:10

LKB


I referred this link to wirte the data to excel sheet.

https://www.npmjs.com/package/exceljs

for downloading excel sheet I used this code to download the excel sheet.

 var fileName = "Task" + '_Template.xlsx';
    var tempFilePath = __dirname + "\\public\\template\\" + fileName;
    workbook.xlsx.writeFile(tempFilePath).then(function () {
        res.send(fileName);
    });
like image 23
Vinoth Avatar answered Oct 19 '25 07:10

Vinoth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!