我的应用程序中有使用Apache poi库的代码,但问题是他正在读取我不想要的单元格。因为我现在想要如何仅从第33行开始读取单元格。我该怎么做?
File file = new File(inFileName);
Workbook workBook = WorkbookFactory.create(file);
Sheet sheet = workBook.getSheetAt(0);
Iterator<Row> rowIter = sheet.rowIterator();
while(rowIter.hasNext()){
Row myRow =rowIter.next();
Cell cell1 = myRow.getCell(0);
Cell cell2 = myRow.getCell(1);
...
Iterator<Cell> cellIter = myRow.cellIterator();
while(cellIter.hasNext()){
Cell myCell = cellIter.next();
}
//bd.addAnimalls(cell1.toString(),cell2.toString());
Toast.makeText(getApplicationContext(), "on inserting", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:0)
您可以使用sheet.getPhysicalNymberOfRows()
确定物理行的总数,并将其用作上限索引。
然后,使用基于索引的行sheet.rowIterator()
:
sheet.createRow(index)
for (int i = 33; i < sheet.getPhysicalNymberOfRows(); i ++)
{
Row r = sheet.createRow(i);
....
}
答案 1 :(得分:0)
the Apache POI documentation on iterating over rows and cells中介绍了这一点。非常值得阅读文档!
要从Excel第33行开始迭代,即POI第32行(POI行为0),最多为第1400行(作为示例),您需要执行以下操作:
int rowStart = 33-1; // POI rows are 0 based
int rowEnd = Math.min(1400, sheet.getLastRowNum());
final int MY_MINIMUM_COLUMN_COUNT = 10; // Minimum number of columns to consider
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
if (r == null) {
// This whole row is empty
// Handle it as needed
continue;
}
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
} else {
// Do something useful with the cell's contents
}
}
}