1

I'm trying to export excel file using exceljs library. I'm using AngularJS and NodeJS.

Here is my code:

HTML:

<a class="btn m-b-xs btn-info btn-doc" ng-click="exportExcel()" style='background-color: #34495e; margin-left: 5%;'>
</a>

Controller:

$scope.exportExcel = function() {
    $http.post('/api/exportExcel/exportExcel', {"data": $scope.data});
};

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

I've just found this example of code for generating excel just to try get excel file on client side and after that i will create my own file. But for now i just get in log this error

file is written
(node:25624) UnhandledPromiseRejectionWarning: TypeError: res.sendFile is not a function

Does anyone can help me to get excel file in browser after i click on button for export?

UPDATE

controller:

$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:

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

2 Answers 2

2

There were some problems, I have corrected them check and verify.

Definition of Route:-

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.
-1

req and res are not associated with 'exceljs'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.