Excel文件下载使用exceljs在node.js中不起作用 [英] Excel file Download Not working in node.js using exceljs

查看:262
本文介绍了Excel文件下载使用exceljs在node.js中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MEAN Stack的新手.

Hi i am new to MEAN Stack.

当我单击导出按钮时,我想下载Excel文件.

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

我正在使用此参考链接下载excel文件: https://www.npmjs.com /package/exceljs

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

HTML页面

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

         });

     };
}

我不知道该怎么做.这是单击按钮下载excel文件的正确方法吗?

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.

推荐答案

我引用了此链接,将数据写到Excel工作表中.

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

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

要下载excel工作表,我使用此代码下载了excel工作表.

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

这篇关于Excel文件下载使用exceljs在node.js中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆