excel文件下载下来损坏 js_Excel文件下载使用exceljs在node.js中不起作用

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

Export

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.

解决方案

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

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

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值