1

I want to read excel spreadsheet(.xlxs) having testcases preconditions column/expected result column and so on. How do i read multiple columns for specific row. Everyrow(first Column)is a testcase name.

I am able to read one row and two columns, but unsure how to read multiple columns for same row . I also don't want to hardcode column#,row# while reading it. Any suggestions?

 Map<String,String> arrayExcelData = new Hashtable<String,String>();            
    Iterator<Row> rowIterator = sheet.iterator();

    while (rowIterator.hasNext()) 
    {

      Row row = rowIterator.next();

      Cell methodNameCol = row.getCell(0);
      Cell expectedResults = row.getCell(1);

     if (expectedResults != null && methodNameCol !=null) 
      {
  arrayExcelData.put(methodNameCol.getStringCellValue(),expectedResults.getStringCellValue());
        }
     workbook.close();
}     

1 Answer 1

1

The Row class provides methods that return the index of the first and last cell (aka column) in that Row instance. Use these indexes to iterate over the columns:

Row row = rowIterator.next();
short firstCellNumber = row.getFirstCellNum();
short lastCellNumber = row.getLastCellNum();
for(short cellNumber = firstCellNumber; cellNumber < lastCellNumber; cellNumber++) {
    Cell dataCell = row.getCell(cellNumber);
    //Do something with the dataCell here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.