1

Actually, I have written code for reading data from an excel file.

But, I want to read the first row and if required then read the second row which is not happening in my code.

I got all rows data from excel.

How can I get only one row from multiple rows?

I have tried to read data by using cell number, but it is hard coding and I don't want to write hard-coded program.

public Object[][] realExcel(String file , String sheetName, int columnSize) throws IOException {

    @SuppressWarnings("resource")
    XSSFWorkbook excel = new XSSFWorkbook(file);

    XSSFSheet sheet = excel.getSheet(sheetName);
    int rowSize = sheet.getLastRowNum();

    String xslData1;
    double xslNumericData;
    XSSFCell checkData;

    Object[][] data = new Object[rowSize][columnSize];

    for (int i = 1; i <= rowSize; i++) {

        for (int j = 0; j <= columnSize; j++) {
            checkData = sheet.getRow(i).getCell(j);

            if (checkData != null) {
                CellType type = sheet.getRow(i).getCell(j).getCellTypeEnum();

                if (type == CellType.STRING) {
                    xslData1 = sheet.getRow(i).getCell(j).getStringCellValue();
                    data[i - 1][j] = xslData1;
                } else if (type == CellType.NUMERIC) {
                    xslNumericData = sheet.getRow(i).getCell(j).getNumericCellValue();
                    data[i - 1][j] = xslNumericData;
                } else {
                }
            }
        }
    }

    return data;
}
  • Can you share your code to give you the correct answer – Ranjani Oct 1 '18 at 7:17
  • By using this code I can read all data from Excel. But I want to call only first row, if first row is not getting match with my requirements then and then read second row data... – Ashwini Oct 1 '18 at 7:28
0

If you would like to return only 1 specific row then you have to pass the row number as a parameter to the function and also stop using the first loop and your object[][] array will be of size object[1][columnSize].

please check this code.

public Object[][] realExcel(String file , String sheetName, int columnSize, int rowNumber) throws IOException {

        @SuppressWarnings("resource")
        XSSFWorkbook excel = new XSSFWorkbook(file);

        XSSFSheet sheet = excel.getSheet(sheetName);

        int rowSize = sheet.getLastRowNum();

        String xslData1;

        double xslNumericData;


        XSSFCell checkData;


    if(rowNumber <= rowSize){
            Object[][] data = new Object[1][columnSize];


            for (int j = 0; j <= columnSize; j++) {

                checkData = sheet.getRow(rowNumber).getCell(j);

                if (checkData != null) {

                    CellType type = sheet.getRow(rowNumber).getCell(j).getCellTypeEnum();

                    if (type == CellType.STRING) {

                    xslData1 = sheet.getRow(rowNumber).getCell(j).getStringCellValue();

                        data[0][j] = xslData1;



                    } else if (type == CellType.NUMERIC) {


                        xslNumericData = sheet.getRow(rowNumber).getCell(j).getNumericCellValue();

                        data[0][j] = xslNumericData;


                    } else {

                    }
                }
            }
    }else {

        // report that the selected row number is out of the range
    }

    return data;

    }

This function will return a object[][] with specific row number, So after getting the required object[][], Apply your validations if it meets your needs or no, Then if no select the other row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.