Generally, we realize excel export are redirected directly or form submission form, but the background if an exception occurs, a message will be displayed in a new window in the form of text, very friendly, recommended here another implementation, the use of post requests, and you can pass a custom error message:
Front-end axios, responseType to set blob
let that= this
let params = { activitiesId: activitiesId, startTime: startTime, endTime: endTime } axios({ method: 'post', url: '/activityManage/exportExcel', data: params, responseType: 'blob' }).then(resp => { that.downloadFile(resp,that) }).catch(resp => { that.$notify.error(resp.msg || ' Export failed ' ) })
Download method implementations to open a new link, and then place the download button, and automatically click:
the downloadFile (RESP, that) { the let Data = resp.data // herein prompt custom prompt acquired from the header IF (resp.headers [ 'ERRORMSG'] ||! Data) { that. $ notify.error ( decodeURI (resp.headers [ 'ERRORMSG']) || 'export failed' ) return } the let URL = window.URL.createObjectURL ( new new Blob ([Data])) the let Link = document.createElement ( 'A' ) Link. . style.display = 'none' link.href = URL // filename is provided at a rear end link.setAttribute ( 'download', decodeURI ( resp.headers [ 'filename' resp.headers [ 'filename'])) document.body.appendChild(link) link.click() },
Background information settings file with the file name:
the try { HSSFWorkbook HSSFWorkbook = new new HSSFWorkbook () String errorMsg = null ; / * * * excel data encapsulation method thereof, ignored here, customizable Baidu POI * required custom error message, and finally into the header * = errorMsg " export Exception: activity "does not exist * / String fileName =" Excel export data " ; // empty response response.reset (); // set the response of the Header the Response.Addheader (" Content-Disposition "," Attachment; filename = " the URLEncoder.encode + (fileName, "UTF-. 8" )); // message response.setHeader("errorMsg", URLEncoder.encode(errorMsg, "utf-8")); response.setHeader("FileName", URLEncoder.encode(fileName, "utf-8")); response.setContentType("application/vnd.ms-excel;charset=gb2312"); OutputStream os = new BufferedOutputStream(response.getOutputStream()); hssfWorkbook.write(os); hssfWorkbook.close(); os.flush(); os.close(); } catch (Exception e) { log.error("导出分析数据异常:"+e); throw new RuntimeException(e); }