springboot easyexcel 导出excel案例及文件无法打开
文章目錄
- 背景介紹
- easyexcel 表頭及數據
- 表頭
- 簡單
- 合并
- web項目導出方式
- 實踐出真知
- 簡單動態表頭案例
- 遇到的問題
- 前端聯調時excel無法打開
- 下載文件接口有時返回文件流有時返回json的情況
??導出數據是后端經典模塊之一,從原有的poi到現在的easyexcel等等都在努力的幫助開發們縮小數據與excel之間的鴻溝。但是在簡單的導出也會遇到一些有的沒的問題,特地寫個文章記錄下~
背景介紹
一般導出流程圖如下:
#mermaid-svg-XjAAJ7fe8d3cHM3C .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .label text{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .node rect,#mermaid-svg-XjAAJ7fe8d3cHM3C .node circle,#mermaid-svg-XjAAJ7fe8d3cHM3C .node ellipse,#mermaid-svg-XjAAJ7fe8d3cHM3C .node polygon,#mermaid-svg-XjAAJ7fe8d3cHM3C .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-XjAAJ7fe8d3cHM3C .node .label{text-align:center;fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .node.clickable{cursor:pointer}#mermaid-svg-XjAAJ7fe8d3cHM3C .arrowheadPath{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-XjAAJ7fe8d3cHM3C .flowchart-link{stroke:#333;fill:none}#mermaid-svg-XjAAJ7fe8d3cHM3C .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-XjAAJ7fe8d3cHM3C .edgeLabel rect{opacity:0.9}#mermaid-svg-XjAAJ7fe8d3cHM3C .edgeLabel span{color:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-XjAAJ7fe8d3cHM3C .cluster text{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-XjAAJ7fe8d3cHM3C .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-XjAAJ7fe8d3cHM3C text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-XjAAJ7fe8d3cHM3C .actor-line{stroke:grey}#mermaid-svg-XjAAJ7fe8d3cHM3C .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .sequenceNumber{fill:#fff}#mermaid-svg-XjAAJ7fe8d3cHM3C #sequencenumber{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C #crosshead path{fill:#333;stroke:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .messageText{fill:#333;stroke:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-XjAAJ7fe8d3cHM3C .labelText,#mermaid-svg-XjAAJ7fe8d3cHM3C .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-XjAAJ7fe8d3cHM3C .loopText,#mermaid-svg-XjAAJ7fe8d3cHM3C .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-XjAAJ7fe8d3cHM3C .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-XjAAJ7fe8d3cHM3C .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-XjAAJ7fe8d3cHM3C .noteText,#mermaid-svg-XjAAJ7fe8d3cHM3C .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-XjAAJ7fe8d3cHM3C .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-XjAAJ7fe8d3cHM3C .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-XjAAJ7fe8d3cHM3C .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-XjAAJ7fe8d3cHM3C .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C .section{stroke:none;opacity:0.2}#mermaid-svg-XjAAJ7fe8d3cHM3C .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-XjAAJ7fe8d3cHM3C .section2{fill:#fff400}#mermaid-svg-XjAAJ7fe8d3cHM3C .section1,#mermaid-svg-XjAAJ7fe8d3cHM3C .section3{fill:#fff;opacity:0.2}#mermaid-svg-XjAAJ7fe8d3cHM3C .sectionTitle0{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .sectionTitle1{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .sectionTitle2{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .sectionTitle3{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-XjAAJ7fe8d3cHM3C .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C .grid path{stroke-width:0}#mermaid-svg-XjAAJ7fe8d3cHM3C .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-XjAAJ7fe8d3cHM3C .task{stroke-width:2}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskText:not([font-size]){font-size:11px}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-XjAAJ7fe8d3cHM3C .task.clickable{cursor:pointer}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskText0,#mermaid-svg-XjAAJ7fe8d3cHM3C .taskText1,#mermaid-svg-XjAAJ7fe8d3cHM3C .taskText2,#mermaid-svg-XjAAJ7fe8d3cHM3C .taskText3{fill:#fff}#mermaid-svg-XjAAJ7fe8d3cHM3C .task0,#mermaid-svg-XjAAJ7fe8d3cHM3C .task1,#mermaid-svg-XjAAJ7fe8d3cHM3C .task2,#mermaid-svg-XjAAJ7fe8d3cHM3C .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskTextOutside0,#mermaid-svg-XjAAJ7fe8d3cHM3C .taskTextOutside2{fill:#000}#mermaid-svg-XjAAJ7fe8d3cHM3C .taskTextOutside1,#mermaid-svg-XjAAJ7fe8d3cHM3C .taskTextOutside3{fill:#000}#mermaid-svg-XjAAJ7fe8d3cHM3C .active0,#mermaid-svg-XjAAJ7fe8d3cHM3C .active1,#mermaid-svg-XjAAJ7fe8d3cHM3C .active2,#mermaid-svg-XjAAJ7fe8d3cHM3C .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-XjAAJ7fe8d3cHM3C .activeText0,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeText1,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeText2,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeText3{fill:#000 !important}#mermaid-svg-XjAAJ7fe8d3cHM3C .done0,#mermaid-svg-XjAAJ7fe8d3cHM3C .done1,#mermaid-svg-XjAAJ7fe8d3cHM3C .done2,#mermaid-svg-XjAAJ7fe8d3cHM3C .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-XjAAJ7fe8d3cHM3C .doneText0,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneText1,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneText2,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneText3{fill:#000 !important}#mermaid-svg-XjAAJ7fe8d3cHM3C .crit0,#mermaid-svg-XjAAJ7fe8d3cHM3C .crit1,#mermaid-svg-XjAAJ7fe8d3cHM3C .crit2,#mermaid-svg-XjAAJ7fe8d3cHM3C .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-XjAAJ7fe8d3cHM3C .activeCrit0,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeCrit1,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeCrit2,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-XjAAJ7fe8d3cHM3C .doneCrit0,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneCrit1,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneCrit2,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-XjAAJ7fe8d3cHM3C .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-XjAAJ7fe8d3cHM3C .milestoneText{font-style:italic}#mermaid-svg-XjAAJ7fe8d3cHM3C .doneCritText0,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneCritText1,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneCritText2,#mermaid-svg-XjAAJ7fe8d3cHM3C .doneCritText3{fill:#000 !important}#mermaid-svg-XjAAJ7fe8d3cHM3C .activeCritText0,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeCritText1,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeCritText2,#mermaid-svg-XjAAJ7fe8d3cHM3C .activeCritText3{fill:#000 !important}#mermaid-svg-XjAAJ7fe8d3cHM3C .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-XjAAJ7fe8d3cHM3C g.classGroup text .title{font-weight:bolder}#mermaid-svg-XjAAJ7fe8d3cHM3C g.clickable{cursor:pointer}#mermaid-svg-XjAAJ7fe8d3cHM3C g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-XjAAJ7fe8d3cHM3C g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-XjAAJ7fe8d3cHM3C .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-XjAAJ7fe8d3cHM3C .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-XjAAJ7fe8d3cHM3C .dashed-line{stroke-dasharray:3}#mermaid-svg-XjAAJ7fe8d3cHM3C #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C .commit-id,#mermaid-svg-XjAAJ7fe8d3cHM3C .commit-msg,#mermaid-svg-XjAAJ7fe8d3cHM3C .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-XjAAJ7fe8d3cHM3C g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-XjAAJ7fe8d3cHM3C g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-XjAAJ7fe8d3cHM3C g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-XjAAJ7fe8d3cHM3C .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-XjAAJ7fe8d3cHM3C .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-XjAAJ7fe8d3cHM3C .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-XjAAJ7fe8d3cHM3C .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-XjAAJ7fe8d3cHM3C .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-XjAAJ7fe8d3cHM3C .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-XjAAJ7fe8d3cHM3C .edgeLabel text{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-XjAAJ7fe8d3cHM3C .node circle.state-start{fill:black;stroke:black}#mermaid-svg-XjAAJ7fe8d3cHM3C .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-XjAAJ7fe8d3cHM3C #statediagram-barbEnd{fill:#9370db}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-state .divider{stroke:#9370db}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-XjAAJ7fe8d3cHM3C .note-edge{stroke-dasharray:5}#mermaid-svg-XjAAJ7fe8d3cHM3C .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-XjAAJ7fe8d3cHM3C .error-icon{fill:#522}#mermaid-svg-XjAAJ7fe8d3cHM3C .error-text{fill:#522;stroke:#522}#mermaid-svg-XjAAJ7fe8d3cHM3C .edge-thickness-normal{stroke-width:2px}#mermaid-svg-XjAAJ7fe8d3cHM3C .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-XjAAJ7fe8d3cHM3C .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-XjAAJ7fe8d3cHM3C .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-XjAAJ7fe8d3cHM3C .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-XjAAJ7fe8d3cHM3C .marker{fill:#333}#mermaid-svg-XjAAJ7fe8d3cHM3C .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-XjAAJ7fe8d3cHM3C {color: rgba(0, 0, 0, 0.75);font: ;}組裝數據導出組裝數據: 包括excel中表頭、數據及樣式
導出: 文件流
easyexcel 表頭及數據
官方文檔總結的都是經典~
表頭: 分為固定表頭和動態表頭,然后再可以繼續劃分簡單版本和復雜版本,其中復雜版本類似有三四級表頭(以前寫poi硬編碼寫到爆炸=_=||)~ 示例如下(圖片來源):
數據: 隨表頭變更
表頭
簡單
固定表頭,可聲明一個實體類進行定義,如下:
@Data public class TitleData {@ExcelProperty("字符串標題")private String string;@ExcelProperty("日期標題")private Date date;@ExcelProperty("數字標題")private Double doubleData; }動態表頭,只能自己手動寫代碼進行定義,如下:
// 外層數組,一個值代表一列 List<List<String>> headList = new ArrayList<List<String>>(); List<String> head0 = new ArrayList<String>(); head0.add("字符串標題"); headList.add(head0);List<String> head1 = new ArrayList<String>(); head1.add("日期標題"); headList.add(head1);List<String> head2 = new ArrayList<String>(); head2.add("數字標題"); headList.add(head2);合并
@Data public class ComplexHeadData {@ExcelProperty({"主標題", "字符串標題"})private String string;@ExcelProperty({"主標題", "日期標題"})private Date date;@ExcelProperty({"主標題", "數字標題"})private Double doubleData; }web項目導出方式
web項目一般有兩種導出方式:
- 提供下載鏈接(異步):先生成excel并上傳至oss/文件服務器,返回文件鏈接給前端,由前端自行下載
- 文件流(同步):生成excel并塞入response流中
本文主要關注文件流方式,示例代碼如下:
// 這里注意 有同學反應使用swagger 會導致各種問題,請直接用瀏覽器或者用postman response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 這里URLEncoder.encode可以防止中文亂碼 當然和easyexcel沒有關系 String fileName = URLEncoder.encode("測試", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());實踐出真知
簡單動態表頭案例
本案例比較簡單,根據請求參數time來動態定義表頭和創建數據,并以文件流方式返回給前端。 效果如下:
pom中添加依賴:
業務邏輯代碼如下:
@RestController public class ResultController {@PostMapping(value = "/export")public void export(@RequestParam Integer time) {try {HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();response.setHeader("Content-Disposition", "attachment; filename=export.xlsx");// 響應類型,編碼response.setContentType("application/vnd.ms-excel;charset=utf-8");response.setCharacterEncoding("utf-8");EasyExcel.write(response.getOutputStream()).head(getHead(time)).sheet("數據").doWrite(getData(time));} catch (IOException e) {log.error("導出問卷數據失敗!錯誤信息為:{}", e.getMessage());e.printStackTrace();}}/*** 獲取excel標題欄(姓名、手機號、提交時間)* @param questionnaireId* @return*/private List<List<String>> getHead(Integer time) {// 外層數組,一個值代表一列List<List<String>> headList = new ArrayList<List<String>>();List<String> nameList = new ArrayList<String>();nameList.add("姓名");headList.add(nameList);List<String> telList = new ArrayList<String>();telList.add("手機號");headList.add(telList);List<String> submitDTList = new ArrayList<String>();submitDTList.add("提交時間");headList.add(submitDTList);for (int i = 0 ; i < time ; i ++ ) {List<String> list = new ArrayList<String>();list.add("動態標題" + i);headList.add(list);}return headList;}/*** 獲取excel表格數據* @return*/private List<List<Object>> getData(Integer time) {// 將填寫結果 + 提交時間合并為一行數據List<List<Object>> resultList = new ArrayList<List<Object>>();for (int i = 0 ; i < 3 ; i ++ ) {List<Object> list = new ArrayList<Object>();list.add("花卷" + i);list.add("1347000000" + i);list.add("2022-01-18 14:54:00");for (int j = 0 ; j < time ; j ++ ) {list.add("動態內容" + j);}resultList.add(list);}return resultList;}}遇到的問題
前端聯調時excel無法打開
問題描述: 后臺用postman調試都ok,能正常打開excel!但是前端調試時下載的excel提示有破損,無法打開!!!
Tips: 勇敢(不怕死)的質疑前端,你代碼有BUG!
解決方案: 前端需在request和response中添加responseType: blob設置
(以下偽代碼,請重點關注responseType設置即可)
下載文件接口有時返回文件流有時返回json的情況
測試小姐姐提了個單,token超時后,點擊下載按鈕還是能正常導出,excel內容是后端認證系統返回的錯誤碼json串 =_=|| ,網上查找一番也找大佬們討論了下,對于將文件流封裝成系統設定的統一返回json方式不可取,最后抱著前端小姐姐的細腿求解決嘍
解決方法:前端獲取response后根據 content-type進行區分
網上參考文檔
未完待續......
總結
以上是生活随笔為你收集整理的springboot easyexcel 导出excel案例及文件无法打开的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python在职场的用处大吗_Pytho
- 下一篇: 开发工具:Git和SVN有哪些差异,看完