ajax接收excel文件流

前端代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>

<script>
//前端 js jQuery ajax实现文件流下载, 下载doc,xsl等文件内容乱码问题
//https://blog.csdn.net/XUANEER/article/details/108496931?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-3-108496931.pc_agg_new_rank&utm_term=jqueryajax%E6%8E%A5%E6%94%B6%E6%96%87%E4%BB%B6%E6%B5%81&spm=1000.2123.3001.4430

$(document).ready(function(){
	$("button").click(function(){
		const xhr = new XMLHttpRequest();
		//xhr.open('POST', "http://swagger.xjcec.com/system/sysUser/userListExport", true);
		//xhr.open('POST', "http://localhost:9999/system/sysUser/userListExport", true);
		xhr.open('POST', "http://localhost:9999/system/sysUser/tradeRecordExport", true);
		
		xhr.responseType = "blob";  
		
		xhr.onload = () => {
			console.log(xhr);
			console.log(xhr.response);
			const blob = xhr.response;
			const blobUrl = URL.createObjectURL(blob);
			const a = document.createElement('a');
			a.style.display = 'none';
			a.download="用户列表.xls"
			a.href = blobUrl;
			a.target = '_blank';
			a.click();
		}              
		
		var params={
			"pageNum": 1,
			"pageSize": 11,
			"userType": 2,
			"keyWord": "15111111"
		}
		
		var params2 = {"flowType":3,"pageNum":1,"pageSize":10}
		
        xhr.setRequestHeader("Content-Type","application/json;charset=UTF-8");
		xhr.setRequestHeader("Xjcec-Auth","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInRlbGVwaG9uZSI6ImFkbWluIiwidXNlck5hbWUiOiJhZG1pbiIsInR5cGUiOjEsInVzZXJJZCI6IjEiLCJhdXRob3JpdGllcyI6WyJkZWZhdWx0Il0sInBhcmVudElkIjoibnVsbCIsImNsaWVudF9pZCI6InhqY2VjIiwicmVhbE5hbWUiOiJhZG1pbiIsInNjb3BlIjpbImFsbCJdLCJwbGF0Zm9ybV9pZCI6MTAwMDAsImV4cCI6MTY0ODY5NzUxNywianRpIjoiOGZmMGRmYTYtMGJjZC00OTVmLTk1ZTctY2U0NzM2YzkyZWI3In0.7gPbOjtYoWEGqF-Zq5BOok_oEYTc62QHOo7GsN2vhZI");  
		xhr.setRequestHeader("client-type","trade_back");  		
		xhr.send(JSON.stringify(params2));
	})
});

</script>
</head>
<body>

<button>点击</button>

</body>
</html>

后端代码 EasyExcel

  response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("交易记录", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), FinanceTradeFlowVO.FinanceTradeFlowPageVO.class)
                    .registerConverter(new LocalDateTimeConverter()).sheet("sheet").doWrite(financeTradeFlowPageVOS);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值