0

How about reading the row with multiple values using excel

for instance

reference testcase1 testcase2 testcase3
Name Sam ram cam
Age 20 25 30
sex m F m
place place1 place2 place 3

I have to read

testcase1.name=sam
testcase2.name=ram
testcase3.name=cam 

and age

how can I do this... any help would be appreciated

xavi
  • 65
  • 5
poobaty
  • 3
  • 6

2 Answers2

0

This tutorial might be helpful to use: https://www.javatpoint.com/how-to-read-excel-file-in-java.

Basically, you are using the Apache POI Library to parse the file's values, generating iterators for each row, then for each cell in each row, and dealing with the data that way.

In your case, after importing the relevant Classes, the code snippet will be something like this:

//Input Data for Class
FileInputStream in = new FileInputStream(new File("<File Location>.xls(x)"));

// Create an Apache POI Workbook reading the file
HSSFWorkbook workbook = new HSSFWorkbook(in);

// Use the Workbook to make a sheet
HSSFSheet sheet = workbook.getSheetAt(0); // Replace 0 with your sheet number

List<Row> rowsList = new ArrayList<Row>();
sheet.iterator().forEachRemaining(actualList::add); // Gets a list of all the rows

int i = -1;
String[] names = new String[3];

for (Cell cell : rowsList[1]) { // Start at second row
    if (i++ > 0) {
        names[i] = cell.getStringCellValue();
    }
}

testcase1.name = names[0];
testcase2.name = names[1];
testcase3.name = names[2];

i = -1;
int[] ages = new int[3];

for (Cell cell : rowsList[2]) { // Start at Third Row
    if (i++ > 0) {
        ages[i] = (int) cell.getNumericCellValue();
    }
}

testcase1.name = ages[0];
testcase2.name = ages[1];
testcase3.name = ages[2];

Shourya Bansal
  • 324
  • 2
  • 16
0

Use apache poi library.

First you must read the Excel file in a java object

        FileInputStream myFile = new FileInputStream(new File(FILE_NAME));
        Workbook myWorkbook = new XSSFWorkbook(myFile);
        Sheet datatypeSheet = myWorkbook.getSheetAt(0); //read the first sheet
        Iterator<Row> iterator = datatypeSheet.iterator();

Then you can iterate over each row and in each row you can read each cell (aka column value)

    List<TestCase> myList = new ArrayList();
    int rowNum = 0;
    while (iterator.hasNext()) {

                Row row = iterator.next();
                int columnsSize = row.getLastCellNum();

                //create all objects
                if(rowNum == 0) {
                  for (int i = 1; i <= columnsSize; i++) {
                     myList.add(new TestCase());
                  }
                } else {
                   //initialize property in each row for all objects
                     for (int i = 1; i <= columnsSize; i++) {
                        TestCase testCase = myList.get(i-1);

int cellType = row.getCell(i).getCellType();
switch(cellType) {
    case Cell.CELL_TYPE_NUMERIC:
       testCase.properties.add(new Property(row.getCell(0).getStringCellValue(), 
       String.valueOf(row.getCell(i).getNumericCellValue()));
        break;
    case Cell.CELL_TYPE_STRING:
        testCase.properties.add(new Property(row.getCell(0).getStringCellValue(), 
        row.getCell(i).getStringCellValue());
        break;
}
                  }
               }

             rowNum++;     
             }
System.out.println(myList);

   

  

    public class TestCase {
    public List<Property> properties;

    public TestCase(){
    this.properties = new ArrayList();
                     }
    }

    public class Property {
    String name;
    String value;

     public Property(String name, String value){
     this.name = name;
     this.value = value;
    }
        }

*Edit: For loop must have <= condition instead of <

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • thanks for your code.. I need run through the multiple rows and it will be a Loop.. can we add for loop after adding into the list? could you please help me with that? – poobaty Dec 09 '20 at 18:01
  • You can loop again after the first loop. You should only ask for a new iterator with iterator = datatypeSheet.iterator(). Then you can loop again every row for a second time using a similar code as the above. – Panagiotis Bougioukos Dec 09 '20 at 19:26
  • Not able to add to array it says "Cannot instantiate the type Row" any fix for this – poobaty Dec 09 '20 at 20:40
  • What are you trying to do with Row that brings this error? Do you run the code as posted to this answer or something different? – Panagiotis Bougioukos Dec 09 '20 at 20:52
  • I executed the code pasted by you.. am not able to add to list... Could you say me what is "List myList = new ArrayList(); TestCase means.. – poobaty Dec 09 '20 at 21:11
  • TestCase is a Class that should contain the fields name, age, sex, place so you can group all those information for each object. Check my updated answer. – Panagiotis Bougioukos Dec 09 '20 at 21:18
  • Appreicate your answer, however my in my case , I cannot limit to the fields, it could any number of fields. well you mean to say that i can pass whatever value I needed and get the a value of the field? – poobaty Dec 09 '20 at 21:40
  • See my updated answer. This should do exactly what you plan to do. In the end you will have in each TestCase a dynamic field of properties with name and value. So name of property in your example will be Name in first row, Age in second row ... – Panagiotis Bougioukos Dec 09 '20 at 21:58
  • please mark the updated answer as accepted answer if it has solved the problem – Panagiotis Bougioukos Dec 10 '20 at 11:30
  • hello .. thanks for the response .. Now I dont see any errors in your code. one question How do i Pass get the values in my Tests I would like to use Testcase1.name in my frist test and Testcase2.name in my second testcse ,, how do i get the values there .. to use it as Test data – poobaty Dec 10 '20 at 17:32
  • You should read the list that contains this info. myList.get(0).name will be Testcase1.name because TestCase1 as you see it in your excel will be the first element of the array. – Panagiotis Bougioukos Dec 10 '20 at 17:57
  • this is how am calling in my tests t get the values List myList = new ArrayList(); myList = readclassexcel.readexcel(); for(int i = 0; i < myList.size(); i++) { System.out.println("testdata from excel"+myList.get(i)); } where readexcelis the method name . however it is returning null with exception. – poobaty Dec 10 '20 at 18:27
  • one last issue is java.lang.IllegalStateException: Cannot get a text value from a numeric cell I can use Dataformatter, however am not sure how to use it ,,could you please help me on this. – poobaty Dec 10 '20 at 21:26
  • check updated answer. It should fix the problem. – Panagiotis Bougioukos Dec 10 '20 at 21:39
  • Thanks for the answer am getting The method getCellTypeEnum() is undefined for the type Cell and for the cell it is coming as Cannot switch on a value of type CellType. Only convertible int values, strings or enum variables are and where should we pass the Numeric and String – poobaty Dec 10 '20 at 21:53
  • try to fix what is not compatible by reading here https://stackoverflow.com/questions/39993683/alternative-to-deprecated-getcelltype – Panagiotis Bougioukos Dec 10 '20 at 22:02
  • Thank you ,, where to pass NUMERIC and STRING in Switch case. and thank you so much for fixing all the issues – poobaty Dec 10 '20 at 22:17
  • Your excel file contains numeric values on the second row of AGE. If you convert your excel to contain only string values then the library will not have any issue. If you wish to have your excel file as it is you have to make a switch case and check before reading. If the cell contain a numeric value you must read it with getNumericCellValue() and if it has a string value you must retrieve it with getStringCellValue() – Panagiotis Bougioukos Dec 10 '20 at 22:19
  • NUMERIC cannot be resolved to a variable STRING cannot be resolved to a variable .. how to close this... i just copy and paste your code.. – poobaty Dec 10 '20 at 22:37
  • thank you so much again.. the code is working like charm.... however how can i get the value like Testcase1.name = Sam Testcase2.name = Ram . Testcase3.name = Cam in my class ..i need to use the values for my selenium tests – poobaty Dec 11 '20 at 01:44