服务器端返回excel文件流,vue+axios导出excel文件

3 篇文章 0 订阅
// 前端代码

// 方法1
import fileDownload from 'js-file-download';// 需要安装 npm install js-file-download --save

axios({
	method: 'post',
	url: url,
	data: postData,
	responseType: 'arraybuffer',
}).then(function (res) {
    // 方法1
    let fileName = res.headers['content-disposition'].match(/filename=(\S*)/)[1];
    fileDownload(res.data,fileName);
	
    // 方法2
    // let blob = new Blob([res.data], {type: "application/vnd.ms-excel"}); 
    // let objectUrl = URL.createObjectURL(blob); 
    // window.location.href = objectUrl; 
 
}).catch(function (error) {

});

// php代码

ob_clean();
//直接生成一个文件
$name = "入职人员个人信息表.xlsx";
$objWriter =\PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('myexchel.xlsx');
header('Content-Type:application/vnd.ms-excel');
header('Access-Control-Expose-Headers: Content-Disposition');// 加上这句,axios才能获取到header中的content-disposition
header('Content-Disposition:attachment;filename='.$name);
header('Cache-Control:max-age=0');

$objWriter =\PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值