1

I have an excel file which contains various information and also contains a table structure which has two headers unit code and description.So now i am able to read the whole excel file using POI,but my problem is that i need to read only the two values of the coloumns of the table in excel.But i am not able to properly doing that,Here is my code what i have done so far.

public class ReadExcelFileToList {

public static void main(String[] args) throws IOException {

    InputStream input = null;

    try {

        input = new FileInputStream(
                "C:\\Users\\jeet.chatterjee\\Downloads\\unitUploadFormat.xlsx");

        System.out.println("file is found");

        XSSFWorkbook wb = new XSSFWorkbook(input);
        XSSFSheet sheet = wb.getSheetAt(0);
        int rowCount = 0;
        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();
            System.out.println("\n");
            if (rowCount == 3) {
                Iterator cells = row.cellIterator();

                while (cells.hasNext()) {

                    XSSFCell cell = (XSSFCell) cells.next();
                    if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
                        System.out.print(cell.getStringCellValue()
                                + "     ");

                    /*
                     * else if (XSSFCell.CELL_TYPE_STRING ==
                     * cell.getCellType())
                     * System.out.print(cell.getStringCellValue() +
                     * "     "); else if (XSSFCell.CELL_TYPE_STRING ==
                     * cell.getCellType())
                     * System.out.print(cell.getStringCellValue() +
                     * "     ");
                     */

                    else
                        System.out.print("Unknown cell type");

                }
                rowCount++;
            }
        }

    } catch (IOException ex) {

        ex.printStackTrace();
    } finally {
        input.close();
    }

}

}

i am using sheet.getLastRowNum() to get my desired result,But i am not getting it somebody please help.here is my excel file . This is my excel file i just want to get kg & kilogram values and so on ,all the items which will be putted there will be extrated.

lucifer
  • 2,297
  • 18
  • 58
  • 100

2 Answers2

0

The error in you code is explained by the API for getLastRowNum

Returns: last row contained n this sheet (0-based)

so you want

if(rowCount == 4){

but of course if there are empty rows, the result may also not be what you want.

Edit

By why make it so difficult.

Use

XSSFRow row = sheet.getRow(4);
XSSFCell cell = row.getCell(1)
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0
 while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();
            if(rowCount==3){
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {
                XSSFCell cell = (XSSFCell) cells.next();
                    System.out.print(cell.getStringCellValue()+"\t");
                }
            }
            System.out.println();
            rowCount++;
        }

like what Scary Wombat said, use your own counter for rows.

Balaji Krishnan
  • 1,007
  • 3
  • 12
  • 29