javaseleniumapache-poixssfpoi-hssf

How to Validate the String and it's Value in excel through Java?


Am new to Java and I have challenge in validating the excel file.Please help me.

I want to validate that 'Hyderabad' is the location Name and '1' is the location ID from excel(pls screenshot below)through JAVA .Am able to read all the cell values.But I want to verify whether Location Name is Hyderabad or not(Similarly , the location ID).Please suggest me here.

enter image description here

if (fileType.equalsIgnoreCase("Excel")) {
            Workbook wb = new XSSFWorkbook(f);

            for (int i = 0; i < wb.getNumberOfSheets(); i++) {

                System.out.println("Sheet name: " + wb.getSheetName(i));
                Sheet sh=wb.getSheetAt(i);

                if (wb.getSheetName(i).matches("A|B|C|D"))
                {
                    int rowCount = sh.getLastRowNum() - sh.getFirstRowNum();

                    for (int j = 0; j < rowCount + 1; j++) {

                        Row row = sh.getRow(j);

                        for (int k = 0; k < row.getLastCellNum(); k++) {

                            System.out.print(row.getCell(k).getStringCellValue() + "|| ");

                        }
                }
            }

        }

Solution

  • if (fileType.equalsIgnoreCase("Excel")) { Workbook wb = new XSSFWorkbook(f);

            for (int i = 0; i < wb.getNumberOfSheets(); i++) {
    
                System.out.println("Sheet name: " + wb.getSheetName(i));
                Sheet sh=wb.getSheetAt(i);
    
                if (wb.getSheetName(i).matches("A|B|C|D"))
                {
                    int rowCount = sh.getLastRowNum() - sh.getFirstRowNum();
    
                    for (int j = 0; j < rowCount + 1; j++) {
    
                        Row row = sh.getRow(j);
    
    
                        if(row.getCell(0).getStringCellValue().contains("Location Name")){
                        {
                            if(row.getCell(1).getStringCellValue().equalsIgnoreCase("Hydrabad")){
                                System.out.print("Location name is matches with Hydrabad");
                            }
                            else{
                                System.out.print("Location name isn't matches with Hydrabad");
                            }
                        }
    
                        if(row.getCell(0).getStringCellValue().contains("Location ID")){
                        {
                            if(row.getCell(1).getStringCellValue().equalsIgnoreCase("1")){
                                System.out.print("Location id matches");
                            }
                            else{
                                System.out.print("Location id mismatches");
                            }
                        }
                }
    
        }