后端代碼
/** * 小票數據導出 */ @RequestMapping(value = "/export.do" ) public ResponseEntity<byte[]> receiptRawExport(HttpServletRequest request, @RequestBody FilterReceiptDto dto) throws IOException { // 干了很多活 HSSFWorkbook workbook = ExcelUtils.excel(newlist,requestContext); ByteArrayOutputStream output = new ByteArrayOutputStream(); workbook.write(output); return new ResponseEntity<byte[]>(output.toByteArray(), headers, HttpStatus.CREATED); }
前端代碼
that.$axios({ // 用axios發送post請求 method: 'post', // 請求地址 url: '/export.do', // 參數 data: { orgId: 1 }, // 表明返回服務器返回的數據類型 responseType: 'blob' }) .then((res) => { // 處理返回的文件流 debugger; const content = res.data const blob = new Blob([content]) const fileName = '導出.xls' if ('download' in document.createElement('a')) { // 非IE下載 const elink = document.createElement('a') elink.download = fileName elink.style.display = 'none' elink.href = URL.createObjectURL(blob) document.body.appendChild(elink) elink.click() URL.revokeObjectURL(elink.href) // 釋放URL 對象 document.body.removeChild(elink) } else { // IE10+下載 navigator.msSaveBlob(blob, fileName) } })