Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

angularjs - Excel download is not working in MEAN stack app

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

When i click the download button, I want to create a Excel file with dynamic data from data base and download it (Note: Don't want to create and store the excel file into physical path and then download it).

Html Code

<button ng-click="exportData()" class="btn btn-sm btn-primary btn-create">Download</button>

Controller Code

 $scope.exportData = function () {
     $http.get('/Getexcel').then(function (response) {
     });
 };

Server code

 const ExcelConfig = require('./public/Models/Schema/excel');
    app.get('/Getexcel', ExcelConfig.getexceldata);

note : ExcelConfig contain path of schema code

excel.js code

// Require library 
var xl = require('excel4node');
const tempfile = require('tempfile');

// Create a new instance of a Workbook class 
var wb = new xl.Workbook();

// Add Worksheets to the workbook 
var ws = wb.addWorksheet('MaterialFlowSheet');
// Create a reusable style 
var style = wb.createStyle({
    font: {
        color: '#FF0800',
        size: 12
    },
    numberFormat: '$#,##0.00; ($#,##0.00); -'
});

exports.getexceldata = (req, res) => {
    ws.cell(1, 1).string('BatchId').style(style);
    ws.cell(1, 2).string('Source').style(style);
    ws.column(2).setWidth(50);
    ws.row(1).setHeight(20);
    ws.cell(1, 3).string('Destination').style(style);
    ws.column(3).setWidth(50);
    ws.row(1).setHeight(20);
    ws.cell(1, 4).string('DistanceBetweenTwoBuilding').style(style);
    ws.column(4).setWidth(25);
    ws.row(1).setHeight(20);
    ws.cell(1, 5).string('SKU').style(style);
    ws.cell(1, 6).string('UOM').style(style);
    ws.cell(1, 7).string('QtyToBeDelivered').style(style);
    ws.column(7).setWidth(20);
    ws.row(1).setHeight(20);
    ws.cell(1, 8).string('CartType').style(style);
wb.write('Excel.xlsx');
res.download('Excel.xlsx');
};

I have tried with above sample file download. So when I click the Download button, the file is not downloading instead nothing happened in UI. But I'm Getting downloaded If I access this URL http://localhost:8080/Getexcel. Can anyone give the solution to download the excel file by clicking the Download button?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must send response as file

app.get('/spreadsheet', function(req, res, next) {
  res.setHeader('Content-disposition', `attachment;filename=data.xls`);
  res.setHeader('Content-type', 'application/vnd.ms-excel');
  res.charset = 'UTF-8';
  res.status(200).end(xls);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...