0
点赞
收藏
分享

微信扫一扫

jquery ajax接收excel文件流

凉夜lrs 2022-03-24 阅读 73

前端代码

<!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);

举报

相关推荐

0 条评论