-
java讀寫excel 需要引入一下jar包
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.0.1' compile group: 'org.apache.poi', name: 'poi', version: '4.0.1' -
讀取excel表格
Workbook workbook= WorkbookFactory.create(new FileInputStream("C:\\Users\\Administrator\\Desktop\\新建 XLSX 工作表.xlsx")); for(int i=0;i<workbook.getNumberOfSheets();i++){ //getNumberOfSheets()獲取sheet的數(shù)量 Sheet hssfSheet=workbook.getSheetAt(i); if(hssfSheet.getPhysicalNumberOfRows()==0){ //getPhysicalNumberOfRows獲取有效的行總數(shù)空行不算 continue; } Row headRow=hssfSheet.getRow(hssfSheet.getFirstRowNum()); //表頭必填所有數(shù)據(jù)以表頭為準(zhǔn)getFirstRowNum()獲取有數(shù)據(jù)的行號(hào)有可能數(shù)據(jù)從第2行開始 for(int row=hssfSheet.getFirstRowNum();row<=hssfSheet.getLastRowNum();row++){ // getLastRowNum為最后一行的下標(biāo) Row rowData=hssfSheet.getRow(row); if(rowData!=null) { if(rowData.getPhysicalNumberOfCells()==0){ continue; } for (int cell = headRow.getFirstCellNum(); cell < headRow.getLastCellNum(); cell++) { //以表頭的列數(shù)為準(zhǔn),超出表頭的數(shù)據(jù)忽略,具體表具體分析這里采用標(biāo)準(zhǔn) getLastCellNum 最后該行該列的下標(biāo)+1 Cell cellData = rowData.getCell(cell); if (cellData == null) { //這里以表頭的的下標(biāo)來(lái)取值有可能 該行該列沒有值 continue; } CellType cellType=cellData.getCellType(); if(cellType.compareTo(CellType.NUMERIC)==0){ if(HSSFDateUtil.isCellDateFormatted(cellData)){ //判斷是否是時(shí)間 System.out.print(cellData.getDateCellValue() + " "); }else{ double doubleValue=cellData.getNumericCellValue(); long longValue=(long)doubleValue; if(longValue==doubleValue){ System.out.print(longValue + " "); //去除后面小數(shù)點(diǎn)0 }else{ System.out.print(doubleValue); } } }else if(cellType.compareTo(CellType.STRING)==0){ System.out.print(cellData.getStringCellValue() + " "); }else if(cellType.compareTo(CellType.BOOLEAN)==0){ System.out.print(cellData.getBooleanCellValue()); }else if(cellType.compareTo(CellType.FORMULA)==0){ }else { } } System.out.println(""); } }excel源文件執(zhí)行結(jié)果
java excel讀取
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

