I have read several posts on how to read Excel files but to be honest I got stuck and I think I need something simpler.
What I need to do is 2 things. Using the following sample table:
- Get column names in a list of Strings. For example:
columnList = Name, FirstName, Email, Date
- Get values from a specific column: For example:
parameter = FirstName (or Index, columns position are fixed)
valuesList = Smith, Gomez, Brown
I hope someone can help me. Thanks!
This is what I have tried: (I took it from other forums and modified it. This code is to get cell values from a column.
@Keyword def getCellValuesList(String xlFilePath, String sheetName, String colName) {
def fis = new FilelnputStream(xlFilePath);
def workbook = new XSSFWbrkbook(fis);
def sheet def row def cell def totalRowCount String rsltValuesListTemp = 'value'
List < String > rsltValuesList = new ArrayList < > () fis.close();
WebUI.comment("rsltValuesListTemp = $rsltValuesListTemp")
try {
int col_Num = -1;
sheet = workbook.getSheet(sheetName);
row = sheet.getRow(0);
for (int i = 0; i < row.getLastCellNum(); i++) {
if (row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
col_Num = i;
}
row = sheet.getRow(1);
WebUI.comment("Current row $row")
while (rsltValuesListTemp != '') {
cell = row.getCell(col_Num);
WebUI.comment("Cell value $cell")
if (cell.getCellTypeEnum() == CellType.STRING) {
rsltValuesListTemp = cell.getStringCellValue();
rsaValuesList.add(rsaValuesListTemp)
WebUI.comment("rsaValuesList = $rsltValuesList")
} else if (cell.getCellTypeEnum() == CellType.NUMER || cell.getCellTypeEnum() == CellType.FORMULA) {
rsltValuesListTemp = String.valueOf(cell.getNumericCellValue());
if (HSSFDateUtil.isCettDateFormatted(cell)) {
DateFormat df = new SimpleDateFormat("dd/MM/yy");
Date date = cell.getDateCellValue();
rsltValuesListTemp = df.getStringCellValue(format(date));
}
rsltValuesList.add(rsltValuesListTemp)
WebUI.comment("rsltValuesList = $rsltValuesList")
} else if (cell.getCellTypeEnum() == CellType.BLANK)
rsltValuesListTemp = "";
else
rsltValuesListTemp = String.vaueOf(cell.getSooleanCellValue());
WebUI.comment("row = $row")
WebUI.comment("rsaValuesList = $rsltValuesList")
}
} catch (Exception e) {
e.printStackTrace();
return "column " + colName + " does not exist in Excel";
}
WebUI.comment("List values for column $colName = $rsltValuesListTemp")
}
I Can only get the first values, but loop seems to fail.
