akash
akash

Reputation: 105

How to read excel file from particular row?

I want to read from a particular row say after nth row of excel sheet in Java. I don't want to check whether the cells have value in it or not. I just want to skip some of the starting rows say n and start reading from n+1 th row.

How would I achieve that goal?

    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);

Upvotes: 0

Views: 6053

Answers (2)

Amudha Dhanapal
Amudha Dhanapal

Reputation: 1

public void skipNthRowMethod( int skipRowNumbers ) {
    int rowNum = 0;

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

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

Use the above way will help you to skip the row numbers.

check and let me know in case of any more help you need.

Upvotes: 0

gaurav5430
gaurav5430

Reputation: 13872

something like this?

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

also you can use

sheet.getLastRowNum(); to get the last row.

Upvotes: 1

Related Questions