如何使用Apache POI API读取xlsx
文件时获取最后一列的索引?
有一个getLastRowNum
方法,但我找不到任何与列数相关的内容......
编辑:
我正在处理XLSX
个文件
答案 0 :(得分:21)
我认为你必须遍历各行并检查每一行HSSFRow.getLastCellNum()
。
答案 1 :(得分:7)
检查每一行并致电Row.getLastCellNum()
最大单元格编号是最后一列编号。
Row r = sheet.getRow(rowNum);
int maxCell= r.getLastCellNum();
答案 2 :(得分:2)
要了解具有任何行值的最后一列,首先需要获取该行,然后您可以找到具有值的最后一列
语法:
sheet.getrow(RowNumber).getLastCellNum();
RowNumber
- >是您想要知道具有值
答案 3 :(得分:1)
尝试此功能:
private void maxExcelrowcol() {
int row, col, maxrow, maxcol;
//Put file name here for example filename.xls
String filename = "filename.xls";
static String TAG = "ExelLog";
//you can use 'this' in place of context if you want
Context context = getApplicationContext();
try {
// Creating Input Stream
File file = new File(context.getExternalFilesDir(null), filename);
FileInputStream myInput = new FileInputStream(file);
// Create a POIFSFileSystem object
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
// Create a workbook using the File System
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
// Get the first sheet from workbook
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
//Row iterator
Iterator rowIter = mySheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next();
//Cell iterator for iterating from cell to next cell of a row
Iterator cellIter = myRow.cellIterator();
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
row = myCell.getRowIndex();
col = myCell.getColumnIndex();
if (maxrow < row) {
maxrow = row;
}
if (maxcol < col) {
maxcol = col;
}
}
}
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}