Excel file Download Not working in node.js using exceljs

牧云@^-^@ 提交于 2019-12-10 18:06:40

问题


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.


回答1:


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);
    });


来源:https://stackoverflow.com/questions/44451884/excel-file-download-not-working-in-node-js-using-exceljs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!