溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ajax實現(xiàn)excel報表導出的案例分析

發(fā)布時間:2020-07-18 15:01:13 來源:億速云 閱讀:135 作者:清晨 欄目:web開發(fā)

這篇文章主要介紹ajax實現(xiàn)excel報表導出的案例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

利用ajax實現(xiàn)excel報表導出【解決亂碼問題】,供大家參考,具體內容如下

背景

項目中遇到一個場景,要導出一個excel報表。由于需要token驗證,所以不能用a標簽;由于頁面復雜,所以不能使用表單提交。初步考慮前端使用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('導出失敗,導出的內容為空!');
   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請自行百度)

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總體設置
   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();
   
 ......
 
}

測試結果

excel能正常導出,但下載下來的excel全是亂碼。經(jīng)過各種找答案,整理了一下可能是以下原因導致:

1、后端未設置字符集,或者在spring框架的過濾器中統(tǒng)一設置了字符集;
2、前端頁面未設置字符集編碼;
3、需要在ajax中添加 request.responseType = “arraybuffer”;

經(jīng)過不斷測試,我的應該是第三點導致。但在jquery ajax 中添加后仍然不起作用,亂碼問題始終無法解決。

第二版

主要代碼

前端,使用原生的ajax。后端未變動。

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('導出失敗,導出的內容為空!');
  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));

測試結果

下載的excel不再亂碼,原生ajax中使用 “arraybuffer” 使用是生效的。

以上是ajax實現(xiàn)excel報表導出的案例分析的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI