springboot easyexcel 导出excel案例及文件无法打开
文章目錄
- 背景介紹
- easyexcel 表頭及數(shù)據(jù)
- 表頭
- 簡(jiǎn)單
- 合并
- web項(xiàng)目導(dǎo)出方式
- 實(shí)踐出真知
- 簡(jiǎn)單動(dòng)態(tài)表頭案例
- 遇到的問(wèn)題
- 前端聯(lián)調(diào)時(shí)excel無(wú)法打開(kāi)
- 下載文件接口有時(shí)返回文件流有時(shí)返回json的情況
??導(dǎo)出數(shù)據(jù)是后端經(jīng)典模塊之一,從原有的poi到現(xiàn)在的easyexcel等等都在努力的幫助開(kāi)發(fā)們縮小數(shù)據(jù)與excel之間的鴻溝。但是在簡(jiǎn)單的導(dǎo)出也會(huì)遇到一些有的沒(méi)的問(wèn)題,特地寫(xiě)個(gè)文章記錄下~
背景介紹
一般導(dǎo)出流程圖如下:
#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: ;}組裝數(shù)據(jù)導(dǎo)出組裝數(shù)據(jù): 包括excel中表頭、數(shù)據(jù)及樣式
導(dǎo)出: 文件流
easyexcel 表頭及數(shù)據(jù)
官方文檔總結(jié)的都是經(jīng)典~
表頭: 分為固定表頭和動(dòng)態(tài)表頭,然后再可以繼續(xù)劃分簡(jiǎn)單版本和復(fù)雜版本,其中復(fù)雜版本類(lèi)似有三四級(jí)表頭(以前寫(xiě)poi硬編碼寫(xiě)到爆炸=_=||)~ 示例如下(圖片來(lái)源):
數(shù)據(jù): 隨表頭變更
表頭
簡(jiǎn)單
固定表頭,可聲明一個(gè)實(shí)體類(lèi)進(jìn)行定義,如下:
@Data public class TitleData {@ExcelProperty("字符串標(biāo)題")private String string;@ExcelProperty("日期標(biāo)題")private Date date;@ExcelProperty("數(shù)字標(biāo)題")private Double doubleData; }動(dòng)態(tài)表頭,只能自己手動(dòng)寫(xiě)代碼進(jìn)行定義,如下:
// 外層數(shù)組,一個(gè)值代表一列 List<List<String>> headList = new ArrayList<List<String>>(); List<String> head0 = new ArrayList<String>(); head0.add("字符串標(biāo)題"); headList.add(head0);List<String> head1 = new ArrayList<String>(); head1.add("日期標(biāo)題"); headList.add(head1);List<String> head2 = new ArrayList<String>(); head2.add("數(shù)字標(biāo)題"); headList.add(head2);合并
@Data public class ComplexHeadData {@ExcelProperty({"主標(biāo)題", "字符串標(biāo)題"})private String string;@ExcelProperty({"主標(biāo)題", "日期標(biāo)題"})private Date date;@ExcelProperty({"主標(biāo)題", "數(shù)字標(biāo)題"})private Double doubleData; }web項(xiàng)目導(dǎo)出方式
web項(xiàng)目一般有兩種導(dǎo)出方式:
- 提供下載鏈接(異步):先生成excel并上傳至oss/文件服務(wù)器,返回文件鏈接給前端,由前端自行下載
- 文件流(同步):生成excel并塞入response流中
本文主要關(guān)注文件流方式,示例代碼如下:
// 這里注意 有同學(xué)反應(yīng)使用swagger 會(huì)導(dǎo)致各種問(wèn)題,請(qǐng)直接用瀏覽器或者用postman response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 這里URLEncoder.encode可以防止中文亂碼 當(dāng)然和easyexcel沒(méi)有關(guān)系 String fileName = URLEncoder.encode("測(cè)試", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());實(shí)踐出真知
簡(jiǎn)單動(dòng)態(tài)表頭案例
本案例比較簡(jiǎn)單,根據(jù)請(qǐng)求參數(shù)time來(lái)動(dòng)態(tài)定義表頭和創(chuàng)建數(shù)據(jù),并以文件流方式返回給前端。 效果如下:
pom中添加依賴(lài):
業(yè)務(wù)邏輯代碼如下:
@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");// 響應(yīng)類(lèi)型,編碼response.setContentType("application/vnd.ms-excel;charset=utf-8");response.setCharacterEncoding("utf-8");EasyExcel.write(response.getOutputStream()).head(getHead(time)).sheet("數(shù)據(jù)").doWrite(getData(time));} catch (IOException e) {log.error("導(dǎo)出問(wèn)卷數(shù)據(jù)失敗!錯(cuò)誤信息為:{}", e.getMessage());e.printStackTrace();}}/*** 獲取excel標(biāo)題欄(姓名、手機(jī)號(hào)、提交時(shí)間)* @param questionnaireId* @return*/private List<List<String>> getHead(Integer time) {// 外層數(shù)組,一個(gè)值代表一列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("手機(jī)號(hào)");headList.add(telList);List<String> submitDTList = new ArrayList<String>();submitDTList.add("提交時(shí)間");headList.add(submitDTList);for (int i = 0 ; i < time ; i ++ ) {List<String> list = new ArrayList<String>();list.add("動(dòng)態(tài)標(biāo)題" + i);headList.add(list);}return headList;}/*** 獲取excel表格數(shù)據(jù)* @return*/private List<List<Object>> getData(Integer time) {// 將填寫(xiě)結(jié)果 + 提交時(shí)間合并為一行數(shù)據(jù)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("動(dòng)態(tài)內(nèi)容" + j);}resultList.add(list);}return resultList;}}遇到的問(wèn)題
前端聯(lián)調(diào)時(shí)excel無(wú)法打開(kāi)
問(wèn)題描述: 后臺(tái)用postman調(diào)試都o(jì)k,能正常打開(kāi)excel!但是前端調(diào)試時(shí)下載的excel提示有破損,無(wú)法打開(kāi)!!!
Tips: 勇敢(不怕死)的質(zhì)疑前端,你代碼有BUG!
解決方案: 前端需在request和response中添加responseType: blob設(shè)置
(以下偽代碼,請(qǐng)重點(diǎn)關(guān)注responseType設(shè)置即可)
下載文件接口有時(shí)返回文件流有時(shí)返回json的情況
測(cè)試小姐姐提了個(gè)單,token超時(shí)后,點(diǎn)擊下載按鈕還是能正常導(dǎo)出,excel內(nèi)容是后端認(rèn)證系統(tǒng)返回的錯(cuò)誤碼json串 =_=|| ,網(wǎng)上查找一番也找大佬們討論了下,對(duì)于將文件流封裝成系統(tǒng)設(shè)定的統(tǒng)一返回json方式不可取,最后抱著前端小姐姐的細(xì)腿求解決嘍
解決方法:前端獲取response后根據(jù) content-type進(jìn)行區(qū)分
網(wǎng)上參考文檔
未完待續(xù)......
總結(jié)
以上是生活随笔為你收集整理的springboot easyexcel 导出excel案例及文件无法打开的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python在职场的用处大吗_Pytho
- 下一篇: 开发工具:Git和SVN有哪些差异,看完