简体   繁体   English

Excel 文件下载 使用 exceljs 在 node.js 中不起作用

[英]Excel file Download Not working in node.js using exceljs

Hi i am new to MEAN Stack.嗨,我是 MEAN Stack 的新手。

I want to download the excel file when i click the export button.我想在单击导出按钮时下载 excel 文件。

I am using this reference link to download the excel file : https://www.npmjs.com/package/exceljs我正在使用此参考链接下载 excel 文件: 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.通过单击按钮下载excel文件的方法是否正确。

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.我参考此链接将数据写入 Excel 表。

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

for downloading excel sheet I used this code to download the excel sheet.为了下载excel表,我使用此代码下载了excel表。

 var fileName = "Task" + '_Template.xlsx';
    var tempFilePath = __dirname + "\\public\\template\\" + fileName;
    workbook.xlsx.writeFile(tempFilePath).then(function () {
        res.send(fileName);
    });

Do not use不使用

workbook.xlsx.writeFile()

writeFile() method is for saving file to hard disk. writeFile() 方法用于将文件保存到硬盘。

Instead, use write() method.相反,使用 write() 方法。 This method writes file to a stream.此方法将文件写入流。

res object is a Writable stream. res 对象是一个 Writable 流。 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.因为您已经将 excel 文件流水线化为 res 对象。 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 对象上添加 https 标头。

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-Type 通知 Web 浏览器数据类型是什么。

Content-Disposition notify Web Browser that this data will be saved to hard disk. Content-Disposition 通知 Web 浏览器此数据将保存到硬盘。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM