// 导出Excel的函数
exportExcel = () => {
// 请求的URL,假设你的服务器端已经设置好了用于导出Excel的接口
const url = 'http://yourserver.com/api/export';
// 这里可以添加需要传递的参数
const params = {
// 参数示例
id: 123
};
// 将参数转换为查询字符串
const queryString = new URLSearchParams(params).toString();
// 设置请求的头部信息,可以根据需要设置Content-Type等
const headers = new Headers({
'Content-Type': 'application/json',
'Accept': 'application/vnd.ms-excel', // 指定接收Excel文件
});
// 发送请求
fetch(`${url}?${queryString}`, { headers })
.then(response => response.blob()) // 转换响应为Blob
.then(blob => {
// 创建一个临时的URL,用于下载文件
const url = window.URL.createObjectURL(new Blob([blob]));
const link = document.createElement('a');
link.href = url;
// 设置下载的文件名
link.setAttribute('download', 'export.xls');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch(error => console.error('Error downloading the file:', error));
};
react fetch get请求传参导出excel
最新推荐文章于 2024-05-30 15:43:06 发布