Excel下载在MEAN堆栈应用程序中不起作用 [英] Excel download is not working in MEAN stack app

查看:118
本文介绍了Excel下载在MEAN堆栈应用程序中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击按钮时,我想下载excel文件。



当我点击下载按钮时,我想使用动态数据从数据创建一个Excel文件基础并下载它(注意:不要创建和存储Excel文件到物理路径,然后下载它)。


Html代码




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




控制器代码




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




服务器代码




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




注意:
ExcelConfig包含模式代码的路径

excel.js代码




  /需要库
var xl = require('excel4node');
const tempfile = require('tempfile');

//创建一个Workbook类的新实例
var wb = new xl.Workbook();

//将工作表添加到工作簿
var ws = wb.addWorksheet('MaterialFlowSheet');
//创建一个可重用的样式
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');
};

我已尝试上面的示例文件下载。所以当我点击下载按钮,文件不会下载,而没有发生在UI中。但是如果我访问这个URL http:// localhost:8080 / Getexcel ,我正在下载。
任何人都可以通过点击下载按钮来解决方案下载excel文件?

解决方案

您必须发送回复文件

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


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?

解决方案

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

这篇关于Excel下载在MEAN堆栈应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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