需求
excel表格=>List<Map<K,V>>的形式
技術需要:
io流,poi,List和Map集合基本用法
流程
<style>#mermaid-svg-vIjwxQlqoh353Wuy .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-vIjwxQlqoh353Wuy .label text{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .node rect,#mermaid-svg-vIjwxQlqoh353Wuy .node circle,#mermaid-svg-vIjwxQlqoh353Wuy .node ellipse,#mermaid-svg-vIjwxQlqoh353Wuy .node polygon,#mermaid-svg-vIjwxQlqoh353Wuy .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-vIjwxQlqoh353Wuy .node .label{text-align:center;fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .node.clickable{cursor:pointer}#mermaid-svg-vIjwxQlqoh353Wuy .arrowheadPath{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-vIjwxQlqoh353Wuy .flowchart-link{stroke:#333;fill:none}#mermaid-svg-vIjwxQlqoh353Wuy .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-vIjwxQlqoh353Wuy .edgeLabel rect{opacity:0.9}#mermaid-svg-vIjwxQlqoh353Wuy .edgeLabel span{color:#333}#mermaid-svg-vIjwxQlqoh353Wuy .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-vIjwxQlqoh353Wuy .cluster text{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy 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-vIjwxQlqoh353Wuy .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-vIjwxQlqoh353Wuy text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-vIjwxQlqoh353Wuy .actor-line{stroke:grey}#mermaid-svg-vIjwxQlqoh353Wuy .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-vIjwxQlqoh353Wuy .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-vIjwxQlqoh353Wuy #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-vIjwxQlqoh353Wuy .sequenceNumber{fill:#fff}#mermaid-svg-vIjwxQlqoh353Wuy #sequencenumber{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy #crosshead path{fill:#333;stroke:#333}#mermaid-svg-vIjwxQlqoh353Wuy .messageText{fill:#333;stroke:#333}#mermaid-svg-vIjwxQlqoh353Wuy .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-vIjwxQlqoh353Wuy .labelText,#mermaid-svg-vIjwxQlqoh353Wuy .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-vIjwxQlqoh353Wuy .loopText,#mermaid-svg-vIjwxQlqoh353Wuy .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-vIjwxQlqoh353Wuy .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-vIjwxQlqoh353Wuy .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-vIjwxQlqoh353Wuy .noteText,#mermaid-svg-vIjwxQlqoh353Wuy .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-vIjwxQlqoh353Wuy .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-vIjwxQlqoh353Wuy .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-vIjwxQlqoh353Wuy .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-vIjwxQlqoh353Wuy .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy .section{stroke:none;opacity:0.2}#mermaid-svg-vIjwxQlqoh353Wuy .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-vIjwxQlqoh353Wuy .section2{fill:#fff400}#mermaid-svg-vIjwxQlqoh353Wuy .section1,#mermaid-svg-vIjwxQlqoh353Wuy .section3{fill:#fff;opacity:0.2}#mermaid-svg-vIjwxQlqoh353Wuy .sectionTitle0{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .sectionTitle1{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .sectionTitle2{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .sectionTitle3{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-vIjwxQlqoh353Wuy .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy .grid path{stroke-width:0}#mermaid-svg-vIjwxQlqoh353Wuy .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-vIjwxQlqoh353Wuy .task{stroke-width:2}#mermaid-svg-vIjwxQlqoh353Wuy .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy .taskText:not([font-size]){font-size:11px}#mermaid-svg-vIjwxQlqoh353Wuy .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-vIjwxQlqoh353Wuy .task.clickable{cursor:pointer}#mermaid-svg-vIjwxQlqoh353Wuy .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vIjwxQlqoh353Wuy .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vIjwxQlqoh353Wuy .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vIjwxQlqoh353Wuy .taskText0,#mermaid-svg-vIjwxQlqoh353Wuy .taskText1,#mermaid-svg-vIjwxQlqoh353Wuy .taskText2,#mermaid-svg-vIjwxQlqoh353Wuy .taskText3{fill:#fff}#mermaid-svg-vIjwxQlqoh353Wuy .task0,#mermaid-svg-vIjwxQlqoh353Wuy .task1,#mermaid-svg-vIjwxQlqoh353Wuy .task2,#mermaid-svg-vIjwxQlqoh353Wuy .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-vIjwxQlqoh353Wuy .taskTextOutside0,#mermaid-svg-vIjwxQlqoh353Wuy .taskTextOutside2{fill:#000}#mermaid-svg-vIjwxQlqoh353Wuy .taskTextOutside1,#mermaid-svg-vIjwxQlqoh353Wuy .taskTextOutside3{fill:#000}#mermaid-svg-vIjwxQlqoh353Wuy .active0,#mermaid-svg-vIjwxQlqoh353Wuy .active1,#mermaid-svg-vIjwxQlqoh353Wuy .active2,#mermaid-svg-vIjwxQlqoh353Wuy .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-vIjwxQlqoh353Wuy .activeText0,#mermaid-svg-vIjwxQlqoh353Wuy .activeText1,#mermaid-svg-vIjwxQlqoh353Wuy .activeText2,#mermaid-svg-vIjwxQlqoh353Wuy .activeText3{fill:#000 !important}#mermaid-svg-vIjwxQlqoh353Wuy .done0,#mermaid-svg-vIjwxQlqoh353Wuy .done1,#mermaid-svg-vIjwxQlqoh353Wuy .done2,#mermaid-svg-vIjwxQlqoh353Wuy .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-vIjwxQlqoh353Wuy .doneText0,#mermaid-svg-vIjwxQlqoh353Wuy .doneText1,#mermaid-svg-vIjwxQlqoh353Wuy .doneText2,#mermaid-svg-vIjwxQlqoh353Wuy .doneText3{fill:#000 !important}#mermaid-svg-vIjwxQlqoh353Wuy .crit0,#mermaid-svg-vIjwxQlqoh353Wuy .crit1,#mermaid-svg-vIjwxQlqoh353Wuy .crit2,#mermaid-svg-vIjwxQlqoh353Wuy .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-vIjwxQlqoh353Wuy .activeCrit0,#mermaid-svg-vIjwxQlqoh353Wuy .activeCrit1,#mermaid-svg-vIjwxQlqoh353Wuy .activeCrit2,#mermaid-svg-vIjwxQlqoh353Wuy .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-vIjwxQlqoh353Wuy .doneCrit0,#mermaid-svg-vIjwxQlqoh353Wuy .doneCrit1,#mermaid-svg-vIjwxQlqoh353Wuy .doneCrit2,#mermaid-svg-vIjwxQlqoh353Wuy .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-vIjwxQlqoh353Wuy .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-vIjwxQlqoh353Wuy .milestoneText{font-style:italic}#mermaid-svg-vIjwxQlqoh353Wuy .doneCritText0,#mermaid-svg-vIjwxQlqoh353Wuy .doneCritText1,#mermaid-svg-vIjwxQlqoh353Wuy .doneCritText2,#mermaid-svg-vIjwxQlqoh353Wuy .doneCritText3{fill:#000 !important}#mermaid-svg-vIjwxQlqoh353Wuy .activeCritText0,#mermaid-svg-vIjwxQlqoh353Wuy .activeCritText1,#mermaid-svg-vIjwxQlqoh353Wuy .activeCritText2,#mermaid-svg-vIjwxQlqoh353Wuy .activeCritText3{fill:#000 !important}#mermaid-svg-vIjwxQlqoh353Wuy .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-vIjwxQlqoh353Wuy g.classGroup text .title{font-weight:bolder}#mermaid-svg-vIjwxQlqoh353Wuy g.clickable{cursor:pointer}#mermaid-svg-vIjwxQlqoh353Wuy g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-vIjwxQlqoh353Wuy g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-vIjwxQlqoh353Wuy .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-vIjwxQlqoh353Wuy .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-vIjwxQlqoh353Wuy .dashed-line{stroke-dasharray:3}#mermaid-svg-vIjwxQlqoh353Wuy #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy .commit-id,#mermaid-svg-vIjwxQlqoh353Wuy .commit-msg,#mermaid-svg-vIjwxQlqoh353Wuy .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-vIjwxQlqoh353Wuy g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-vIjwxQlqoh353Wuy g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-vIjwxQlqoh353Wuy g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-vIjwxQlqoh353Wuy .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-vIjwxQlqoh353Wuy .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-vIjwxQlqoh353Wuy .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-vIjwxQlqoh353Wuy .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-vIjwxQlqoh353Wuy .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-vIjwxQlqoh353Wuy .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-vIjwxQlqoh353Wuy .edgeLabel text{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vIjwxQlqoh353Wuy .node circle.state-start{fill:black;stroke:black}#mermaid-svg-vIjwxQlqoh353Wuy .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-vIjwxQlqoh353Wuy #statediagram-barbEnd{fill:#9370db}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-state .divider{stroke:#9370db}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-vIjwxQlqoh353Wuy .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-vIjwxQlqoh353Wuy .note-edge{stroke-dasharray:5}#mermaid-svg-vIjwxQlqoh353Wuy .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-vIjwxQlqoh353Wuy .error-icon{fill:#522}#mermaid-svg-vIjwxQlqoh353Wuy .error-text{fill:#522;stroke:#522}#mermaid-svg-vIjwxQlqoh353Wuy .edge-thickness-normal{stroke-width:2px}#mermaid-svg-vIjwxQlqoh353Wuy .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-vIjwxQlqoh353Wuy .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-vIjwxQlqoh353Wuy .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-vIjwxQlqoh353Wuy .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-vIjwxQlqoh353Wuy .marker{fill:#333}#mermaid-svg-vIjwxQlqoh353Wuy .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}</style>
<style>#mermaid-svg-vIjwxQlqoh353Wuy {
color: rgba(0, 0, 0, 0.75);
font: ;
}</style>
new File 指向xhl表格
io:FileInputStream File傳入
poi:New XSSFWorkbook 輸入流傳入
生成sheet
生成Rows
遍歷每一個Cell getCellValue
list .add
一步一步來 ?? 組裝io流:
InputStream is = new FileInputStream ( new File ( path) ) //path:xhl路徑
?? poi WorkBook一本書
XSSFWorkbook xssfWorkbook = null;
try {
xssfWorkbook = new XSSFWorkbook ( is) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
?? poi Sheet一本書的每一頁
XSSFSheet xssfSheet= null;
xssfSheet = xssfWorkbook. getSheetAt ( 0 ) ; //我的xhl只有一頁
List< Map< String, String> > list = new ArrayList < Map< String, String> > ( ) ; //一會用這個集合存我們需要的資料
?? poi Rows 標題行
XSSFRow titleRow = xssfSheet. getRow ( 0 ) ; //標題行 XSSRow也是個集合
?? poi 回圈遍歷每一行 以及每一個單元格 雙層嵌套
for ( int rowIndex = 1 ; rowIndex < xssfSheet. getPhysicalNumberOfRows ( ) ; rowIndex++ ) {
XSSFRow xssfRow = xssfSheet. getRow ( rowIndex) ;
//空值判斷
if ( xssfRow == null) {
continue ;
}
Map< String, String> map = new LinkedHashMap < String, String> ( ) ;
//回圈取每個單元格(cell)的資料
for ( int cellIndex = 0 ; cellIndex < xssfRow. getPhysicalNumberOfCells ( ) ; cellIndex++ ) {
XSSFCell titleCell = titleRow. getCell ( cellIndex) ;
XSSFCell xssfCell = xssfRow. getCell ( cellIndex) ;
map. put ( getString ( titleCell) , getString ( xssfCell) ) ;
}
list. add ( map) ;
}
截至到這 我們需要的List集合就拿到了 可以進行你需要的操作了 我封裝成方法了 著急干活的小伙伴可以直接c+v👇
public static List< Map< String, String> > getMapArr ( InputStream is) {
XSSFWorkbook xssfWorkbook = null;
try {
xssfWorkbook = new XSSFWorkbook ( is) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
//定義作業表
XSSFSheet xssfSheet;
xssfSheet = xssfWorkbook. getSheetAt ( 0 ) ;
List< Map< String, String> > list = new ArrayList < Map< String, String> > ( ) ;
//定義行
//默認第一行為標題行,index = 0
XSSFRow titleRow = xssfSheet. getRow ( 0 ) ;
//回圈取每行的資料
for ( int rowIndex = 1 ; rowIndex < xssfSheet. getPhysicalNumberOfRows ( ) ; rowIndex++ ) {
XSSFRow xssfRow = xssfSheet. getRow ( rowIndex) ;
if ( xssfRow == null) {
continue ;
}
Map< String, String> map = new LinkedHashMap < String, String> ( ) ;
//回圈取每個單元格(cell)的資料
for ( int cellIndex = 0 ; cellIndex < xssfRow. getPhysicalNumberOfCells ( ) ; cellIndex++ ) {
XSSFCell titleCell = titleRow. getCell ( cellIndex) ;
XSSFCell xssfCell = xssfRow. getCell ( cellIndex) ;
map. put ( getString ( titleCell) , getString ( xssfCell) ) ;
}
list. add ( map) ;
}
return list;
}
按說到這,就沒啥可繼續往下說的了,但我不滿意,我要將這個xhl表格里的東西,存到資料庫里實作一個批量添加,說干就干走起 額 寫不開了有點 下篇接上吧=>客戶都excel表格最終進入服務器資料庫