Estoy utilizando Excel para datos de entrada para mis scripts de automatización de pruebas y estoy datos de lectura capaces de sobresalir cuando sé la fila exacta y la columna a leer. El reto que estoy enfrentando es cuando tengo celdas combinadas en la hoja. Por ejemplo, la muestra de prueba de datos de Excel
Aquí NombreDeSecuenciaDeComandos e iteración son mis claves principales para identificar un conjunto único de datos para mi guión. Así que mi pregunta aquí es:
- Quiero a buscar toda la ReferenceSetName con respecto a un NombreDeSecuenciaDeComandos, y la iteración es decir, para Script de acceso, la iteración 1: tengo a buscar conjunto ABC1 Ref, juego ABC2 Ref, juego ABC3 Ref
- Quiero a buscar toda la PackageName con respecto a un NombreDeSecuenciaDeComandos, iteración, es decir, para ReferenceSet y Script de acceso, la iteración 1, juego ReferenceSet ABC1 Ref: Tengo que ir a buscar ABC1, ABC2, ABC3
Actualmente continuación es el método - getEntireCellValue () que estoy utilizando para recuperar los datos desde Excel y necesito ayuda para resolver los anteriores problemas 2. Cualquier tipo de apoyo es muy apreciado.
public void getExcelRowNum() {
boolean found = false;
String scriptCell = null, iterationCell = null;
try {
@SuppressWarnings("rawtypes")
Iterator iterator = sheet.rowIterator();
while (iterator.hasNext()) {
Row row = (Row) iterator.next();
scriptCell = row.getCell(1).toString().trim();
iterationCell = row.getCell(2).toString().trim();
if (row.getCell(2).getCellTypeEnum() == CellType.NUMERIC)
iterationCell = iterationCell.substring(0, iterationCell.indexOf(".")).trim();
if ((scriptCell.equals(scriptName) && iterationCell.equals(String.valueOf(iteration).trim()))
|| (scriptCell.equals(scriptName) && Integer.parseInt(iterationCell) == iteration)) {
rowNum = row.getRowNum();
found = true;
break;
}
}
if (rowNum == -1 || found == false)
throw new Exception("Please check the test name: " + scriptName + " or the iteration: " + iteration
+ " in the test data sheet");
row = sheet.getRow(0);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void getExcelColNum(String colName) {
boolean found = false;
try {
for (int i = 0; i < row.getLastCellNum(); i++) {
if (row.getCell(i).getStringCellValue().trim().equals(colName.trim())) {
col_Num = i;
found = true;
break;
}
}
if (col_Num == -1 || found == false)
throw new Exception("Please check the column name: " + colName + " in the test data sheet");
}
catch (Exception e) {
e.printStackTrace();
}
}
public void getCell() {
try {
row = sheet.getRow(rowNum);
cell = row.getCell(col_Num);
}
catch (Exception e) {
e.printStackTrace();
}
}
//Prior to calling this method. I am connecting to the excel sheet which
is in .xlsx or xls format
public String getEntireCellValue(String sheetName, String colName) {
try {
sheet = workbook.getSheet(sheetName);
getExcelRowNum();
getExcelColNum(colName);
getCell();
if (cell.getCellTypeEnum() == CellType.STRING)
return cell.getStringCellValue().trim();
else if (cell.getCellTypeEnum() == CellType.BLANK)
return null;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
public int getNumOfMergedRows() {
int rowsMerged = 0;
try {
for(int i = 0; i < sheet.getNumMergedRegions(); i++) {
CellRangeAddress range = sheet.getMergedRegion(i);
if (range.getFirstRow() <= rowNum && range.getLastRow() >=
rowNum) {
++rowsMerged;
}
}
System.out.println("Number of rows merged are: " + rowsMerged);
}
catch (Exception e) {
e.printStackTrace();
}
return rowsMerged;
}
PS Lo que estoy haciendo aquí es, estoy tratando de buscar el número de filas fusionadas para un script por ejemplo, 6 filas se fusionan para Script de acceso y luego encontrar el número de células dentro de esos 6 filas para obtener el nombre conjunto de referencia (3 celdas). Nota: Cuando llamo el método anterior - getNumOfMergedRows () para determinar el número de filas se fusionó para Script de acceso, estoy consiguiendo 4 como salida en lugar de 6.
El debajo de código va a determinar el número de células fusionadas en una columna - colName, con begining fila como startingRow
public int getNumOfMergedRows(String colName, int startingRow) {
int rowsMerged = 0, col = 0;
XSSFRow mergedRow = null;
XSSFCell mergedCell = null;
try {
col = getExcelColNum(colName);
for (int i = startingRow + 1; i < sheet.getPhysicalNumberOfRows(); i++) {
mergedRow = sheet.getRow(i);
mergedCell = mergedRow.getCell(col);
if (mergedCell.getCellTypeEnum() == null || mergedCell.getCellTypeEnum() == CellType.BLANK)
rowsMerged++;
else
break;
}
rowsMerged++;
}
catch (Exception e) {
e.printStackTrace();
}
logger.info(rowsMerged + " rows are merged in columne" + colName + " for " + scriptName + " script");
return rowsMerged;
}