Excel file Upload Doesn't work in node.js with exceljs
Hi I'm new to MEAN Stack.
I want to download an excel file when I click the export button.
I am using this 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 do not know how to do that. it is correct to load the excel file by clicking the button.
when i click the button i get router no error is defined. Can anyone please solve my problem.
+3
source to share
1 answer
I referenced this link to get the data removed.
https://www.npmjs.com/package/exceljs
to load excel sheet i used this code to load excel sheet.
var fileName = "Task" + '_Template.xlsx';
var tempFilePath = __dirname + "\\public\\template\\" + fileName;
workbook.xlsx.writeFile(tempFilePath).then(function () {
res.send(fileName);
});
0
source to share