成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術(shù)文章
文章詳情頁

ajax實(shí)現(xiàn)excel報(bào)表導(dǎo)出

瀏覽:270日期:2022-06-11 16:50:10

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

背景

項(xiàng)目中遇到一個場景,要導(dǎo)出一個excel報(bào)表。由于需要token驗(yàn)證,所以不能用a標(biāo)簽;由于頁面復(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請自行百度)

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();
   
 ......
 
}

測試結(jié)果

excel能正常導(dǎo)出,但下載下來的excel全是亂碼。經(jīng)過各種找答案,整理了一下可能是以下原因?qū)е拢?/p>

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

經(jīng)過不斷測試,我的應(yīng)該是第三點(diǎn)導(dǎo)致。但在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("導(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));

測試結(jié)果

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

總結(jié)

“arraybuffer” 這個參數(shù)導(dǎo)致的excel導(dǎo)出亂碼,在原生的ajax中設(shè)置是有效的,在jquery的ajax中暫時還沒找到生效的方式。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。

標(biāo)簽: Ajax
相關(guān)文章:
主站蜘蛛池模板: 欧美成本人视频 | 夜色邦合成福利网站 | 一道本久久 | 男人扒开腿躁女人j | 2022麻豆福利午夜久久 | 成年人激情视频 | 大臿蕉香蕉大视频成人 | v片在线看| 成年人免费在线视频观看 | 国产欧美在线播放 | 亚洲一区二区三区在线网站 | 欧美一级片网 | 一区二区三区不卡视频 | 亚洲精品成人中文网 | 中国hd高清╳xxx | 免费的三级网站 | 最新国产午夜精品视频成人 | 中国老妇色xxxxx | 美女被男人桶到嗷嗷叫爽网站 | 国产成人在线影院 | 亚洲成a人片在线网站 | 欧美日韩国产片 | 久久久久久网址 | 窝窝女人体国产午夜视频 | 久久精品视频在线观看 | 黄色欧美网站 | 久久久久久久久久久视频国内精品视频 | a级片在线免费播放 | 毛片手机在线视频免费观看 | 日韩毛片欧美一级a | 2022年国产精品久久久久 | 免费视频观看在线www日本 | 久色网址 | 免费香蕉成视频成人网 | 久久国产精品岛国搬运工 | 欧美日韩一级大片 | 色综合久久久久 | 成年人免费在线视频网站 | 久久精品国产国产精品四凭 | 老师张开腿让我爽了一夜视频 | 国产精品亚洲综合久久 |