后端controller
c
@PostMapping("/exportPlanProject2")
public void exportActive(@RequestBody USER user, HttpServletResponse httpServletResponse) throws IOException {}
后端service
c
public void exportExcel2(HttpServletResponse response) throws IOException {
OutputStream out = response.getOutputStream();
try {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding(Charsets.UTF_8.name());
String fileName = "首页导出";
fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
wb.write(out);
} catch (Exception e) {
log.error("导出Excel异常{}", e.getMessage());
throw new CustomException("导出Excel失败,请联系网站管理员!");
} finally {
if (wb != null) {
try {
wb.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
前端
c
request({
method: 'post',
url: '/investment/plan/remake/exportPlanProject2',
data: this.queryParams,
responseType: 'blob'
}).then(res => {
console.log('res---',res)
const aLink = document.createElement('a')
var fileName = '文件名称' + moment().format('YYYYMMDDHHmmss') + '.xlsx'
fileName = fileName.replace(/\"/g, '')
const url = window.URL.createObjectURL(res)
console.log('url---',url)
aLink.href = url
aLink.download = fileName
aLink.click()
aLink.remove()
URL.revokeObjectURL(url)
})