日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java 获取excel最后一行_查找Excel电子表格中的最后一行

發布時間:2025/4/17 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 获取excel最后一行_查找Excel电子表格中的最后一行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

確切知道的唯一方法是測試行 . 這是我用于同一問題的解決方案:

int lastRowIndex = -1;

if( sheet.getPhysicalNumberOfRows() > 0 )

{

// getLastRowNum() actually returns an index, not a row number

lastRowIndex = sheet.getLastRowNum();

// now, start at end of spreadsheet and work our way backwards until we find a row having data

for( ; lastRowIndex >= 0; lastRowIndex-- ){

Row row = sheet.getRow( lastRowIndex );

if( row != null ){

break;

}

}

}

注意:這不會檢查看似空但不是的行,例如其中包含空字符串的單元格 . 為此,您需要一個更完整的解決方案,例如:

private int determineRowCount()

{

this.evaluator = workbook.getCreationHelper().createFormulaEvaluator();

this.formatter = new DataFormatter( true );

int lastRowIndex = -1;

if( sheet.getPhysicalNumberOfRows() > 0 )

{

// getLastRowNum() actually returns an index, not a row number

lastRowIndex = sheet.getLastRowNum();

// now, start at end of spreadsheet and work our way backwards until we find a row having data

for( ; lastRowIndex >= 0; lastRowIndex-- )

{

Row row = sheet.getRow( lastRowIndex );

if( !isRowEmpty( row ) )

{

break;

}

}

}

return lastRowIndex;

}

/**

* Determine whether a row is effectively completely empty - i.e. all cells either contain an empty string or nothing.

*/

private boolean isRowEmpty( Row row )

{

if( row == null ){

return true;

}

int cellCount = row.getLastCellNum() + 1;

for( int i = 0; i < cellCount; i++ ){

String cellValue = getCellValue( row, i );

if( cellValue != null && cellValue.length() > 0 ){

return false;

}

}

return true;

}

/**

* Get the effective value of a cell, formatted according to the formatting of the cell.

* If the cell contains a formula, it is evaluated first, then the result is formatted.

*

* @param row the row

* @param columnIndex the cell's column index

* @return the cell's value

*/

private String getCellValue( Row row, int columnIndex )

{

String cellValue;

Cell cell = row.getCell( columnIndex );

if( cell == null ){

// no data in this cell

cellValue = null;

}

else{

if( cell.getCellType() != Cell.CELL_TYPE_FORMULA ){

// cell has a value, so format it into a string

cellValue = this.formatter.formatCellValue( cell );

}

else {

// cell has a formula, so evaluate it

cellValue = this.formatter.formatCellValue( cell, this.evaluator );

}

}

return cellValue;

}

總結

以上是生活随笔為你收集整理的java 获取excel最后一行_查找Excel电子表格中的最后一行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。