Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用exceljs将excel文件导出到客户端_Node.js_Angularjs - Fatal编程技术网

Node.js 使用exceljs将excel文件导出到客户端

Node.js 使用exceljs将excel文件导出到客户端,node.js,angularjs,Node.js,Angularjs,我正在尝试使用exceljs库导出excel文件。我用的是AngularJS和NodeJS 这是我的密码: HTML: NodeJS: const Excel = require('exceljs'); export async function exportExcel(req, res) { try { var workbook = new Excel.Workbook(); var worksheet = workbook.addWorksheet('My Shee

我正在尝试使用exceljs库导出excel文件。我用的是AngularJS和NodeJS

这是我的密码:

HTML:

NodeJS:

const Excel = require('exceljs');

export async function exportExcel(req, res) {
    try {
    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的代码示例,只是为了尝试在客户端获取excel文件,然后我将创建自己的文件。 但现在我只是记录了这个错误

file is written
(node:25624) UnhandledPromiseRejectionWarning: TypeError: res.sendFile is not a function
在我点击按钮导出后,有人能帮我在浏览器中获取excel文件吗

更新

控制器:

$scope.exportExcel = function() {
    $http.post('/api/exportExcel/exportExcel', {"data": $scope.data});
};
$scope.exportExcel = function() {
            $http.post('/api/exportExcel/exportExcel', {"offer": $scope.offer})
            .then(function(response) {
                console.log(response.data);
                var data = response.data,
                blob = new Blob([data], { type: response.headers('content-type') }),
                url = $window.URL || $window.webkitURL;
                $scope.fileUrl = url.createObjectURL(blob);
            });
        };
html:



req和res与“exceljs”没有关联。

有一些问题,我已经纠正了,请检查并验证

路线定义:-

var express = require("express");
var router = express.Router();
var fs = require("fs");
const Excel = require("exceljs");
var path = require("path");

router.get("/", async function(req, res, next) {
  console.log("---InSideFunction---");
  try {
    var workbook = new Excel.Workbook();
    var worksheet = workbook.addWorksheet();

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

    workbook.xlsx
      .writeFile("newSaveeee.xlsx")
      .then(response => {
        console.log("file is written");
        console.log(path.join(__dirname, "../newSaveeee.xlsx"));
        res.sendFile(path.join(__dirname, "../newSaveeee.xlsx"));
      })
      .catch(err => {
        console.log(err);
      });
  } catch (err) {
    console.log("OOOOOOO this is the error: " + err);
  }
});

module.exports = router;

评论不用于扩展讨论;这段对话已经结束。
<a class="btn m-b-xs btn-info btn-doc" ng-click="exportExcel()" ng-href="{{ fileUrl }}" download="table.xlsx">
<i class="fa"></i>Export</a>
var express = require("express");
var router = express.Router();
var fs = require("fs");
const Excel = require("exceljs");
var path = require("path");

router.get("/", async function(req, res, next) {
  console.log("---InSideFunction---");
  try {
    var workbook = new Excel.Workbook();
    var worksheet = workbook.addWorksheet();

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

    workbook.xlsx
      .writeFile("newSaveeee.xlsx")
      .then(response => {
        console.log("file is written");
        console.log(path.join(__dirname, "../newSaveeee.xlsx"));
        res.sendFile(path.join(__dirname, "../newSaveeee.xlsx"));
      })
      .catch(err => {
        console.log(err);
      });
  } catch (err) {
    console.log("OOOOOOO this is the error: " + err);
  }
});

module.exports = router;