简体   繁体   中英

Reading multiple columns for a row from excel, in java?

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM