Java之如何讀取Excel獲取真實行數(shù)
Java讀取Excel獲取真實行數(shù)
剛進入公司開發(fā),熟悉環(huán)境是個很大的難題,今天就接到了一個任務(wù),讓我修改批量上傳excel文件的頁面.
公司采用的是apache提供的包,通過XML文件的映射,把EXCEL表和我們的Model對應(yīng)起來.本來是校驗正確的,結(jié)果莫名其妙到后面就會報空指針異常.
問題的原因:在沒有格式的前提下,getLastRowNum方法能夠正確返回最后一行的位置;getPhysicalNumberOfRows方法能夠正確返回物理的行數(shù);
* 在有格式的前提下,這兩個方法都是不合理的;
* 所以,在做導(dǎo)入excel的時候,建議想要正確獲取行數(shù),可以做一個人為的約定,比如約定導(dǎo)入文件第一列不允許為空,行數(shù)就按照第一列的有效行數(shù)來統(tǒng)計;這樣就能正確獲取到實際想要的行數(shù);
更新版本,因為發(fā)現(xiàn)有時候 存在了加了樣式的邊框,邊框的屬性默認成為了 公式屬性,導(dǎo)致后面空指針,現(xiàn)已修復(fù)
修改版
/**
* 用來得到真實行數(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ù)
導(dǎo)入相關(guān)的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文件需要用到的對象。構(gòu)造時,參數(shù)是一個FileInputStream對象,里面寫上文件的地址。
2、getNumberOfSheets()獲取擁有的sheet總頁數(shù)。
3、getSheetAt(number)操作第幾個工作簿,參數(shù)是第幾個sheet表。此處的參數(shù)索引是從0開始。返回值是一個工作簿對象XSSFSheet,用于操作這個工作簿。
4、工作簿對象.getLastRowNum()。獲取該工作簿一共有多少列。
5、getRow(row_num).getLastCellNum().獲取改行共有多少列。此處行和列的下標(biāo)都是從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的索引下標(biāo)是從0開始的,得到該頁碼的對象
? ? ? ? ? ? ? ? XSSFSheet sheet = xssfWorkbook.getSheetAt(i);
? ? ? ? ? ? ? ? //獲取總共的行數(shù)
? ? ? ? ? ? ? ? int maxRow = sheet.getLastRowNum();
? ? ? ? ? ? ? ? //對每一行進行遍歷
? ? ? ? ? ? ? ? for (int row = 0; row <= maxRow; row++) {
? ? ? ? ? ? ? ? ? ? //getRow(row_num)獲取改行的對象,再.getLastCellNum()獲取該行共有幾列
? ? ? ? ? ? ? ? ? ? //此處與sheet不同的是,該索引下標(biāo)是從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();
? ? ? ? }
? ? }總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Apache?SkyWalking?修復(fù)TTL?timer?失效bug詳解
這篇文章主要為大家介紹了Apache?SkyWalking?修復(fù)TTL?timer?失效bug詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Spring boot中限制 Mybatis SQL日志的大字段輸出問題
文章介紹Springboot中MyBatis SQL日志因大字段輸出導(dǎo)致日志膨脹及JVM內(nèi)存問題,建議通過Logback或Log4j2自定義Converter/Filter實現(xiàn)參數(shù)截斷或脫敏,或調(diào)整日志級別限制調(diào)試信息,本文給大家介紹Spring boot中限制Mybatis SQL日志的大字段輸出問題,感興趣的朋友一起看看吧2025-10-10
使用IntelliJ IDEA查看類的繼承關(guān)系圖形(圖文詳解)
這篇文章主要介紹了使用IntelliJ IDEA查看類的繼承關(guān)系圖形,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Java實戰(zhàn)之圖書管理系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java語言編寫一個圖書管理系統(tǒng),文中采用的技術(shù)有Springboot、SpringMVC、MyBatis、ThymeLeaf 等,需要的可以參考一下2022-03-03

