Vue2 导出Excel + 解决乱码问题 —— axios

请求用的axios(类似ajax问题),找了很多方法,都下载不了文件。

 

以下是解决方法):

1.接口返回的流:

 

2、请求头和返回头:

 

 

方法一、方法二:

2.下载流文件的代码

 

方法一:是用了插件 https://github.com/kennethjiang/js-file-download

方法二:是用了 blob

不管哪种方法,记得设置  responseType  !!!!!

 

附上代码:


 
 
  1. // 导出Excel
  2. exportBill: function () {
  3. let url_post = Vue.prototype.api.apiList.EXPORT_BILL; //请求地址
  4. let params_post = { //参数
  5. orderStartDate: this.timepickerDateFormat( this.rangeTime[ 0]) || this.rangeTime[ 0] || '',
  6. orderEndDate: this.timepickerDateFormat( this.rangeTime[ 1]) || this.rangeTime[ 1] || '',
  7. prodCode: this.prodId,
  8. promoteFlag: this.promotionSiteId,
  9. policyStatusList: this.tableBillStateCheckedData,
  10. };
  11. Vue.axios.post(url_post, params_post, {
  12. responseType: 'arraybuffer'
  13. }).then( (res) => {
  14. let fileName = res.headers[ 'content-disposition'].match( /fushun(\S*)xls/)[ 0];
  15. fileDownload(res.data, fileName); //如果用方法一 ,这里需要安装 npm install js-file-download --save ,然后引用 var fileDownload = require('js-file-download'),使用详情见github;
  16.        // let blob = new Blob([res.data], {type: "application/vnd.ms-excel"});
  17.        // let objectUrl = URL.createObjectURL(blob);
  18.        // window.location.href = objectUrl;
  19. }).catch( function (res) {});
  20. },

 

 
 
 

 

方法三(附加):

1、接口要求: post方法、入参为json格式、出参文件流

* 2、axios:设置返回数据格式为 blob 或者 arraybuffer   ( 注意 )


 
 
  1. axios({ // 用axios发送post请求
  2. method: 'post',
  3. url: ' /serviceTime/exportData', // 请求地址
  4. data: form, // 参数
  5. responseType: 'blob', // 表明返回服务器返回的数据类型
  6. headers: {
  7. 'Content-Type': 'application/json'
  8. }
  9. }).then( res => { // 处理返回的文件流
  10. const blob = new Blob([res.data]); //new Blob([res])中不加data就会返回下图中[objece objece]内容(少取一层)
  11. const fileName = '统计.xlsx'; //下载文件名称
  12. const elink = document.createElement( 'a');
  13. elink.download = fileName;
  14. elink.style.display = 'none';
  15. elink.href = URL.createObjectURL(blob);
  16. document.body.appendChild(elink);
  17. elink.click();
  18. URL.revokeObjectURL(elink.href); // 释放URL 对象
  19. document.body.removeChild(elink);
  20. })

( 上面代码中少取一层的导出文件):

 

 

				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值