-1

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() + "|| ");

                        }
                }
            }

        }
Rahul
  • 759
  • 3
  • 21
  • 43
  • 1
    if(row.getCell(k).getStringCellValue().equals("Hyderabad")) sop("pass") – Murthi Jul 03 '17 at 10:59
  • Thanks for your comment.But I think, this statement will check if the string Hyderabad exists in the given sheet or not. I wanted Location Name == Hyderabad to be validated. Hope it is clear. – Rahul Jul 03 '17 at 11:04
  • What gave you idea that "equals" means "contained somewhere"? – M. Prokhorov Jul 03 '17 at 11:05

1 Answers1

1

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

    }
Murthi
  • 5,299
  • 1
  • 10
  • 15
  • Thank you for detailed code. But it's partially resolved. Able to match with Location Name. Failed to match the location ID.Please see if you can modify it. – Rahul Jul 03 '17 at 12:34
  • you can try with contains instead of equalsIgnoreCase for location id – Murthi Jul 03 '17 at 12:48
  • Okay...Btwn, am still wondering, who downvoted this question. :( Hope it's not a weird question. – Rahul Jul 03 '17 at 13:00