生活随笔
收集整理的這篇文章主要介紹了
vue下载导出Excel案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????上一篇寫了上傳文件,這一篇接著來一個下載導出的案例
????下載Excel文件的一個核心邏輯,需要你自己構建一個函數,如下方downloadGridData,下面是原理:
獲取后端傳遞來的二進制數據流傳遞到封裝的函數當中,需要傳遞二進制數據和文件名將二進制數據流包裹成一個new Blob對象將Blob對象轉化為一個URL資源地址,這個地址時一個本地地址創建一個a標簽,設置隱藏,添加下載屬性,添加到body當中,啟動下載下載完畢之后,刪除a標簽;
注意事項:
????在vue框架當中,數據請求是借助axios的,為此,在發送請求的時候,需要修改responseType,改為arraybuffer,axios默認情況下responseType為json,若是不修改,很可能下載時候會是亂碼,或者為null,還需注意很多項目中對axios進行了封裝攔截返回一直導致獲取不到返回的數據這個一點也各位也要注意;
????下方提供兩個案例:
案例1
downloadGridData
: function () {var _this
= this_this
.$http({method
: 'get',url
: '/proxy/api/v1/disk/testDownload',data
: {},headers
: {'Content-Type': 'application/json','debug': 1,},responseType
:"blob",}).then(response
=> {let blob
= new Blob([response
.data
], {type
: 'application/vnd.ms-excel'})let dateTime
= new Date()let dateTimeStr
= dateTime
.getFullYear() + '-' + dateTime
.getMonth() + '-' + dateTime
.getDay()let filename
= '意向患者-' + dateTimeStr
+ '_' + new Date().getTime() + '.xls'if (typeof window
.navigator
.msSaveBlob
!== 'undefined') {window
.navigator
.msSaveBlob(blob
, filename
)} else {var blobURL
= window
.URL.createObjectURL(blob
)var tempLink
= document
.createElement('a')tempLink
.style
.display
= 'none'tempLink
.href
= blobURLtempLink
.setAttribute('download', filename
)if (typeof tempLink
.download
=== 'undefined') {tempLink
.setAttribute('target', '_blank')}document
.body
.appendChild(tempLink
)tempLink
.click()document
.body
.removeChild(tempLink
)window
.URL.revokeObjectURL(blobURL
)}}).catch((error
) => {console
.log(error
)})},
public void orderDownload(@RequestBody OrderProductsReq req
,HttpServletResponse response
) {String orderToStr
= dateToStr(req
.getOrderTo() == null
? null
: req
.getOrderTo());String orderFromStr
= dateToStr(req
.getOrderFrom() == null
? null
: req
.getOrderFrom());String fromTempStr
= orderFromStr
== null
? "最開始" : orderFromStr
;String toTempStr
= orderToStr
== null
? "現在" : orderToStr
;String fileName
= fromTempStr
+"-"+toTempStr
+"意向記錄:" + System
.currentTimeMillis();try {response
.setHeader("Content-type", "application/vnd.ms-excel");response
.setCharacterEncoding("UTF-8");response
.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName
.getBytes("UTF-8"), "ISO-8859-1") + ".xlsx");orderProductsService
.orderDownload(req
,response
.getOutputStream());} catch (Exception e
) {e
.printStackTrace();}
}
List
<OrderProductsQueryRes> rowList
= result
.getResult();
Workbook wb
= new XSSFWorkbook();
Sheet sheet
= wb
.createSheet("意向患者");
bulidFirstSheet(sheet
, rowList
);
try {wb
.write(outputStream
);
} catch (Exception e
) {e
.printStackTrace();
} finally {try {wb
.close();outputStream
.close();outputStream
.flush();} catch (IOException e
) {e
.printStackTrace();}
}
案例2
- 前臺: 文件名可以通過response.headers獲取設置或者在前端處理
downloadGridData
: function () {var _this
= thisthis.$http
.post(_this
.$host
+ 'admin_product_order_manage/orderListDownload', _this
.search
, {responseType
: 'arraybuffer'}).then(response
=> {let blob
= new Blob([response
.data
], {type
: 'application/vnd.ms-excel'})let dateTime
= new Date()let dateTimeStr
= dateTime
.getFullYear() + '-' + dateTime
.getMonth() + '-' + dateTime
.getDay()let filename
= '意向患者-' + dateTimeStr
+ '_' + new Date().getTime() + '.xls'if (typeof window
.navigator
.msSaveBlob
!== 'undefined') {window
.navigator
.msSaveBlob(blob
, filename
)} else {var blobURL
= window
.URL.createObjectURL(blob
)var tempLink
= document
.createElement('a')tempLink
.style
.display
= 'none'tempLink
.href
= blobURLtempLink
.setAttribute('download', filename
)if (typeof tempLink
.download
=== 'undefined') {tempLink
.setAttribute('target', '_blank')}document
.body
.appendChild(tempLink
)tempLink
.click()document
.body
.removeChild(tempLink
)window
.URL.revokeObjectURL(blobURL
)}}).catch((error
) => {console
.log(error
)})
}
總結
以上是生活随笔為你收集整理的vue下载导出Excel案例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。