null
以下是express服务器上的路由:
let xlsx = require('node-xlsx').default;
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
const data = [[1, 2, 3], ['a', 'b', 'c']];
var buffer = xlsx.build([{ name: "mySheetName", data: data }])
.toString('base64');
res.json(buffer);
});
module.exports = router;
客户端上的React组件:
import React, { useEffect } from 'react';
import axios from 'axios';
const First = () => {
useEffect(() => {
axios({
url: 'http://localhost:5000/excel',
method: 'GET',
responseType: 'blob',
}).then(res => {
const url = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'data.xlsx');
document.body.appendChild(link);
link.click();
});
}, []);
return (
<div>
<h3>First page</h3>
<hr className="gold" />
</div>
);
};
export default First;
问候。
null
请参阅快速示例
null
let myData = "YQ=="
console.log(atob(myData)); // a
为什么不试试呢:https://github.com/eligrey/filesaver.js/我在我的大多数前端解决方案中都使用过这个