ajax實(shí)現(xiàn)excel報(bào)表導(dǎo)出
利用ajax實(shí)現(xiàn)excel報(bào)表導(dǎo)出【解決亂碼問題】,供大家參考,具體內(nèi)容如下
背景
項(xiàng)目中遇到一個(gè)場(chǎng)景,要導(dǎo)出一個(gè)excel報(bào)表。由于需要token驗(yàn)證,所以不能用a標(biāo)簽;由于頁(yè)面復(fù)雜,所以不能使用表單提交。初步考慮前端使用ajax,后端返回流,定義指定的header。
第一版
主要代碼
前端
使用jquery的ajax
var queryParams = {"test":"xxx"}; var url = "xxx"; $.ajax({ type : "POST", //提交方式 url : url,//路徑 contentType: "application/json", data: JSON.stringify(queryParams), beforeSend: function (request) { request.setRequestHeader("Authorization", "xxx"); }, success : function(result) { const blob = new Blob([result], {type:"application/vnd.ms-excel"}); if(blob.size < 1) { alert("導(dǎo)出失敗,導(dǎo)出的內(nèi)容為空!"); return } if(window.navigator.msSaveOrOpenBlob) { navigator.msSaveOrOpenBlob(blob, "test.xls") } else { const aLink = document.createElement("a"); aLink.style.display = "none"; aLink.href = window.URL.createObjectURL(blob); aLink.download = "test.xls"; document.body.appendChild(aLink); aLink.click(); document.body.removeChild(aLink); } } });
后端
使用easypoi(如何使用easypoi請(qǐng)自行百度)
import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; @PostMapping(value = "/download") public void downloadList(@RequestBody Objct obj, HttpServletResponse response) { ...... List<Custom> excelList = new ArrayList<>(); // excel總體設(shè)置 ExportParams exportParams = new ExportParams(); // 指定sheet名字 exportParams.setSheetName("test"); Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Custom.class, excelList); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("test", "utf-8") + ".xls"); OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); ...... }
測(cè)試結(jié)果
excel能正常導(dǎo)出,但下載下來(lái)的excel全是亂碼。經(jīng)過(guò)各種找答案,整理了一下可能是以下原因?qū)е拢?/p>
1、后端未設(shè)置字符集,或者在spring框架的過(guò)濾器中統(tǒng)一設(shè)置了字符集;
2、前端頁(yè)面未設(shè)置字符集編碼;
3、需要在ajax中添加 request.responseType = “arraybuffer”;
經(jīng)過(guò)不斷測(cè)試,我的應(yīng)該是第三點(diǎn)導(dǎo)致。但在jquery ajax 中添加后仍然不起作用,亂碼問題始終無(wú)法解決。
第二版
主要代碼
前端,使用原生的ajax。后端未變動(dòng)。
var xhr = new XMLHttpRequest(); xhr.responseType = "arraybuffer"; xhr.open("POST", url, true); xhr.onload = function () { const blob = new Blob([this.response], {type:"application/vnd.ms-excel"}); if(blob.size < 1) { alert("導(dǎo)出失敗,導(dǎo)出的內(nèi)容為空!"); return; } if(window.navigator.msSaveOrOpenBlob) { navigator.msSaveOrOpenBlob(blob, "test.xls") } else { const aLink = document.createElement("a"); aLink.style.display = "none"; aLink.href = window.URL.createObjectURL(blob); aLink.download = "testxls"; document.body.appendChild(aLink); aLink.click(); document.body.removeChild(aLink); return; } } xhr.setRequestHeader("Authorization", "xxx"); xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(JSON.stringify(queryParams));
測(cè)試結(jié)果
下載的excel不再亂碼,原生ajax中使用 “arraybuffer” 使用是生效的。
總結(jié)
“arraybuffer” 這個(gè)參數(shù)導(dǎo)致的excel導(dǎo)出亂碼,在原生的ajax中設(shè)置是有效的,在jquery的ajax中暫時(shí)還沒找到生效的方式。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
相關(guān)文章:
1. 不使用XMLHttpRequest對(duì)象實(shí)現(xiàn)Ajax效果的方法小結(jié)2. Spring MVC+ajax進(jìn)行信息驗(yàn)證的方法3. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)4. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法5. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟6. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問題7. layui Ajax請(qǐng)求給下拉框賦值的實(shí)例8. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺(tái)】9. SpringMVC+Jquery實(shí)現(xiàn)Ajax功能10. 快速解決ajax返回值給外部函數(shù)的問題
