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

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

问题描述

我想在点击按钮时下载 excel 文件.

当我点击下载按钮时,我想用数据库中的动态数据创建一个Excel文件并下载它(注意:不想创建并存储Excel文件到物理路径然后下载它).

<块引用>

HTML 代码

<块引用>

控制器代码

 $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({字体:{颜色:'#FF0800',尺寸:12},numberFormat: '$#,##0.00;($#,##0.00);——'});export.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天全站免登陆