前端代码
<!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>
$(document).ready(function(){
$("button").click(function(){
const xhr = new XMLHttpRequest();
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);