0

我想从 Java 中的第 n 行 excel 表之后的特定行中读取。我不想检查单元格是否具有价值。我只想跳过一些起始行说 n 并从第 n+1 行开始读取。

我将如何实现这一目标?

    FileInputStream fileInputStream = new FileInputStream(fileName);
    POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
        HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
        HSSFSheet hssfSheet = workBook.getSheetAt(0);
        Iterator<Row> rowIterator = hssfSheet.rowIterator();
        boolean headerRow = true;
        while (rowIterator.hasNext()) {
            if (headerRow == false) {
                HSSFRow hssfRow = (HSSFRow) rowIterator.next()
                Iterator<Cell> iterator = hssfRow.cellIterator();
                List cellTempList = new ArrayList();
                while (iterator.hasNext()) {
                HSSFCell hssfCell = (HSSFCell) iterator.next();
        if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC)
        {DecimalFormat df = new DecimalFormat("#");
            String cellValue=df.format(hssfCell.getNumericCellValue());
            cellTempList.add(cellValue.toString());
        }
     else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_STRING)
     { cellTempList.add(hssfCell.toString());
     }
    }
cellDataList.add(cellTempList);
4

2 回答 2

1

像这样的东西?

for (int rn=startRowNo; rn<=endRowNo; rn++) 
     {
      HSSFRow row = hssfSheet.getRow(rn);
      //do your processing here
     }

你也可以使用

sheet.getLastRowNum();得到最后一行。

于 2014-01-09T08:00:46.897 回答
0
public void skipNthRowMethod( int skipRowNumbers ) {
    int rowNum = 0;

    if ( skipRowNumbers > 0 ) {
        while ( rowIterator.hasNext() ) {
            if( rowNum <= skipRowNumbers ) {
                rowNum++;
                continue;
            }

            rowNum++; // anything for future usage
        }
    }
}

使用上述方式将帮助您跳过行号。

如果您需要更多帮助,请检查并告诉我。

于 2014-01-09T08:11:39.687 回答