如何获取最后一列索引读取excel文件?
问题描述:
如何在使用Apache POI API读取xlsx
文件时获取最后一列的索引?如何获取最后一列索引读取excel文件?
有一个getLastRowNum
方法,但我找不到任何相关的列数...
编辑: 我处理XLSX
文件
答
检查每一行并致电Row.getLastCellNum()
最大单元号是最后一个列号。
Row r = sheet.getRow(rowNum);
int maxCell= r.getLastCellNum();
答
结识有任何行的值,最后一列,首先,你需要获得行,然后你可以找到具有价值
语法的最后一列:
sheet.getrow (ROWNUMBER).getLastCellNum();
ROWNUMBER - >是您要知道,拥有价值
答
最后一列试试这个功能的行号:
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();
}
}
+0
感谢您的回答。如果您还想在代码中添加一些注释,那将会很棒。这有助于人们更好地理解你的答案。 – hofmeister 2018-03-06 13:26:21
对于XLSX文件,你必须改变的唯一事情是HSSF到XSSF。 XSSFRow.getLastCellNum() – 2014-11-19 12:20:26