首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >后台使用excel4node在前端下载excel文件

后台使用excel4node在前端下载excel文件
EN

Stack Overflow用户
提问于 2020-12-04 07:28:11
回答 2查看 655关注 0票数 0

我知道以前有人问过这个问题,但我不能让它工作。后端使用wb.write('filename.xlsx', res)发送文件,但在前端,我只得到一个对象响应。如何让浏览器下载.xlsx文件?

代码语言:javascript
复制
// express backend
const x1 = require('excel4node');
router.get('/excel', async (req, res) => {
  const wb = new x1.Workbook();
  // do some stuff
  wb.write('excel.xlsx', res);
});
代码语言:javascript
复制
// frontend
axis(config).then((res) => {
  // What do I do here? res is an object.  How do I handle this response so that the browser is downloading the file instead?
}).catch(...);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-08 12:06:05

回答前端问题,

您可以按如下方式编写axios代码:

代码语言:javascript
复制
axios.get('URL TO GET FILE BLOB', {
 responseType: 'blob'
}).then(response => {
   let headerLine = response.headers['content-disposition'];
   let startFileNameIndex = headerLine.indexOf('"') + 1
   let endFileNameIndex = headerLine.lastIndexOf('"')
   let filename = headerLine.substring(startFileNameIndex, endFileNameIndex)
   const url = window.URL.createObjectURL(new Blob([response.data], 
   {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}));
   const link = document.createElement('a');

   link.href = url;
   link.setAttribute('download', filename);
   document.body.appendChild(link);
   link.click();
   link.remove();
}).catch(error => {
   console.log(error)
})

后端:

您需要确保正确配置CORS,请检查以下代码以供参考。

代码语言:javascript
复制
const express = require('express');
const xl = require('excel4node');
const cors = require('cors')  //use this

const app = express();
app.use(cors({exposedHeaders: '*'})) //and this
const PORT = process.env.PORT || 3001;

app.get('/', (req, res) => {
  var wb = new xl.Workbook();
 
  // Add Worksheets to the workbook
  var ws = wb.addWorksheet('Sheet 1');
  var ws2 = wb.addWorksheet('Sheet 2');
 
  // Create a reusable style
  var style = wb.createStyle({
    font: {
      color: '#FF0800',
      size: 12,
    },
    numberFormat: '$#,##0.00; ($#,##0.00); -',
  });
 
 // Set value of cell A1 to 100 as a number type styled with 
 ws.cell(1, 1)
    .number(100)
    .style(style);
 
 // Set value of cell B1 to 200 as a number type styled with 

paramaters of style
ws.cell(1, 2)
  .number(200)
  .style(style);
 
// Set value of cell C1 to a formula styled with paramaters of style
ws.cell(1, 3)
  .formula('A1 + B1')
  .style(style);
 
// Set value of cell A2 to 'string' styled with paramaters of style
ws.cell(2, 1)
  .string('string')
  .style(style);
 
// Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size.
ws.cell(3, 1)
  .bool(true)
  .style(style)
  .style({font: {size: 14}});

wb.write('Excel.xlsx', res);
});

app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));

在前端,我们可以从content disposition头中提取文件名和扩展名。这与我们设置wb.write('Excel.xlsx', res);的名称相同

票数 1
EN

Stack Overflow用户

发布于 2020-12-08 11:47:28

尝试使用blob响应类型

代码语言:javascript
复制
axios.request({url,method,responseType: 'blob'})
.then(({ data }) => {
const downloadUrl = window.URL.createObjectURL(new Blob([data]));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65135735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档