Java之如何讀取Excel獲取真實行數(shù)
Java讀取Excel獲取真實行數(shù)
剛進入公司開發(fā),熟悉環(huán)境是個很大的難題,今天就接到了一個任務,讓我修改批量上傳excel文件的頁面.
公司采用的是apache提供的包,通過XML文件的映射,把EXCEL表和我們的Model對應起來.本來是校驗正確的,結果莫名其妙到后面就會報空指針異常.
問題的原因:在沒有格式的前提下,getLastRowNum方法能夠正確返回最后一行的位置;getPhysicalNumberOfRows方法能夠正確返回物理的行數(shù);
* 在有格式的前提下,這兩個方法都是不合理的;
* 所以,在做導入excel的時候,建議想要正確獲取行數(shù),可以做一個人為的約定,比如約定導入文件第一列不允許為空,行數(shù)就按照第一列的有效行數(shù)來統(tǒng)計;這樣就能正確獲取到實際想要的行數(shù);
更新版本,因為發(fā)現(xiàn)有時候 存在了加了樣式的邊框,邊框的屬性默認成為了 公式屬性,導致后面空指針,現(xiàn)已修復
修改版
/** * 用來得到真實行數(shù) * @param sheet * @param flag 需要寫進數(shù)據(jù)庫的列數(shù)用逗號隔開 比如 (Sheet sheet,int 2,int 3);隨意個 * @return * */ public static int findRealRows(Sheet sheet, int... flag) { int row_real = 0; int rows = sheet.getPhysicalNumberOfRows();// 此處物理行數(shù)統(tǒng)計有錯誤, int size = flag.length; try { for (int i = 1; i < rows; i++) { Row row = sheet.getRow(i); int total = 0; ArrayList<Integer> blank =new ArrayList<Integer>(); int type=-1; String s = null; for(int j:flag){ if(!(row.getCell(j) == null)&&row.getCell(j).getCellType()<2){ type=row.getCell(j).getCellType(); row.getCell(j).setCellType(1); } if (row.getCell(j) == null||row.getCell(j).getStringCellValue().matches("^\\s+$")||row.getCell(j).getCellType()>2) { total++; if(!(row.getCell(j) == null)&&row.getCell(j).getCellType()<2){ row.getCell(j).setCellType(type); } blank.add(j); } } System.out.println(s+"我"); // 如果4列都是空說明就該返回 if (total == flag.length) { return row_real; } else if (total == 0) { row_real++; } else { String h=""; for(Integer b:blank){ h=h+"第"+(b+1)+"列"+" "; } throw new BusinessException("第" + (i + 1) + "行" + h + "不能為空"); } } } catch (NullPointerException e) { throw new BusinessException("excel格式異常,請檢查excel格式有無數(shù)據(jù)缺失,無效數(shù)據(jù)行!"); } return row_real; }
方法都這樣,通過約定一個有的ID來進行判斷,可以較快的得到真實的行數(shù) ,以至于后面的集合循環(huán)輸出的話不會出現(xiàn)空指針異常
Java讀取excel數(shù)據(jù)
導入相關的MAVEN依賴。
? ? ? ? <!--excel的依賴--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.apache.poi</groupId> ? ? ? ? ? ? <artifactId>poi</artifactId> ? ? ? ? ? ? <version>4.1.0</version> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.apache.poi</groupId> ? ? ? ? ? ? <artifactId>poi-ooxml</artifactId> ? ? ? ? ? ? <version>4.1.0</version> ? ? ? ? </dependency>
Java代碼
使用說明:
1、XSSFWorkbook是整個操作excel文件需要用到的對象。構造時,參數(shù)是一個FileInputStream對象,里面寫上文件的地址。
2、getNumberOfSheets()獲取擁有的sheet總頁數(shù)。
3、getSheetAt(number)操作第幾個工作簿,參數(shù)是第幾個sheet表。此處的參數(shù)索引是從0開始。返回值是一個工作簿對象XSSFSheet,用于操作這個工作簿。
4、工作簿對象.getLastRowNum()。獲取該工作簿一共有多少列。
5、getRow(row_num).getLastCellNum().獲取改行共有多少列。此處行和列的下標都是從1開始的。
6、getRow(row).getCell(rol)。獲取改行該列的元素。
? ?public static void excelFind() { ? ? ? ? try { ? ? ? ? ? ? XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("src/test/java/postSQL/database/test.xlsx")); ? ? ? ? ? ? //獲取工作簿下sheet的個數(shù) ? ? ? ? ? ? int sheetNum = xssfWorkbook.getNumberOfSheets(); ? ? ? ? ? ? System.out.println("總數(shù)頁碼:"+sheetNum); ? ? ? ? ? ? //遍歷頁碼: ? ? ? ? ? ? for(int i = 0;i<sheetNum;i++) { ? ? ? ? ? ? ? ? System.out.println("讀取第"+(i+1)+"個sheet"); ? ? ? ? ? ? ? ? //sheet的索引下標是從0開始的,得到該頁碼的對象 ? ? ? ? ? ? ? ? XSSFSheet sheet = xssfWorkbook.getSheetAt(i); ? ? ? ? ? ? ? ? //獲取總共的行數(shù) ? ? ? ? ? ? ? ? int maxRow = sheet.getLastRowNum(); ? ? ? ? ? ? ? ? //對每一行進行遍歷 ? ? ? ? ? ? ? ? for (int row = 0; row <= maxRow; row++) { ? ? ? ? ? ? ? ? ? ? //getRow(row_num)獲取改行的對象,再.getLastCellNum()獲取該行共有幾列 ? ? ? ? ? ? ? ? ? ? //此處與sheet不同的是,該索引下標是從1開始的 ? ? ? ? ? ? ? ? ? ? int maxRol = sheet.getRow(row).getLastCellNum(); ? ? ? ? ? ? ? ? ? ? System.out.println("--------第" + row + "行的數(shù)據(jù)如下--------"); ? ? ? ? ? ? ? ? ? ? //遍歷列數(shù) ? ? ? ? ? ? ? ? ? ? for (int rol = 0; rol < maxRol; rol++){ ? ? ? ? ? ? ? ? ? ? ? ? //sheets.getRow(1).getCell(1);獲取元素值 ? ? ? ? ? ? ? ? ? ? ? ? System.out.print(sheet.getRow(row).getCell(rol) + " ?"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? System.out.println(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
半小時實現(xiàn)Java手擼網(wǎng)絡爬蟲框架(附完整源碼)
最近在做一個搜索相關的項目,需要爬取網(wǎng)絡上的一些鏈接存儲到索引庫中,自己寫了一個簡單的網(wǎng)絡爬蟲,感興趣的可以了解一下2021-06-06Mybatis Criteria使用and和or進行聯(lián)合條件查詢的操作方法
這篇文章主要介紹了Mybatis Criteria的and和or進行聯(lián)合條件查詢的方法,本文通過例子給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10Java創(chuàng)建,編輯與刪除Excel迷你圖表的實現(xiàn)方法
迷你圖是Excel工作表單元格中表示數(shù)據(jù)的微型圖表。本文將通過Java代碼示例介紹如何在Excel中創(chuàng)建迷你圖表,以及編輯和刪除表格中的迷你圖表,需要的可以參考一下2022-05-05