前端代码(参考): $("#import").on('click', function(){ var createDate=$("#createdDate").val(); var key1=$("#key1").val(); if(createDate==null||createDate==""){ layer.msg("请选择创建时间段!在点击导出数据)"); return ; } var load = layer.load(); //原生ajax var xhr = new XMLHttpRequest(); //post方式请求后台的路径 xhr.open('post', gatewayServerUrl+'/execute/openApi/importByCreate2', true); //导出的Excel是二进制数据类型,所以设置为blob xhr.responseType = 'blob'; //请求头(key,value),请求头可以设置多个key-value对 xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8'); //返回成功,导出的Excel文件 xhr.onload = function () { if (this.status == 200) { layer.close(load); var blob = this.response; var a = document.createElement('a'); var url = window.URL.createObjectURL(blob); a.href = url; //设置文件名称 a.download = '产品完检管理【'+createDate+'】.xlsx'; a.click(); } } //请求的参数,json格式,后台要用json格式接收 xhr.send(JSON.stringify({ "createDate" : createDate, "key1":key1 })); });
后端代码(参考):文章来源:https://www.toymoban.com/news/detail-794733.html
List<ManuProductCheckExport> list = new ArrayList<>(); OutputStream out = response.getOutputStream(); //文件名需要这样写,不能在setHeader直接写中文名,否则下载的文件名字为空,只有后缀 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("导出数据产品完检", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); ExcelWriter excelWriter = EasyExcelFactory.write(out).build(); EasyExcel.write(response.getOutputStream(), ManuProductCheckExport.class) .sheet("sheet") .doWrite(list); excelWriter.finish(); out.flush(); out.close();
依赖:文章来源地址https://www.toymoban.com/news/detail-794733.html
<!-- easyexcel导入导出 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.7</version> </dependency>
到了这里,关于EasyExcel下载EXCEL文件,后台通过流形式输出到前端浏览器下载方式输出的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!