I am using excel to input data for my test automation scripts and I am able read data from excel when i know the exact row and the column to read. The challenge which I am facing is when I have merged cells in the sheet. E.g. Test data excel sample
我正在使用excel为我的测试自动化脚本输入数据,当我知道要读取的确切行和列时,我能够从excel读取数据。我面临的挑战是当我在工作表中合并单元格时。例如。测试数据优于样本
Here ScriptName and Iteration are my primary keys to identify a unique set of data for my script. So my question here is:
这里ScriptName和Iteration是我为我的脚本识别一组唯一数据的主键。所以我的问题是:
- I want to fetch all the ReferenceSetName with respect to a ScriptName, and Iteration i.e. for Login script, Iteration 1: I have to fetch ABC1 Ref set, ABC2 Ref set, ABC3 Ref set
- 我想获取关于ScriptName的所有ReferenceSetName,以及Iteration,即登录脚本,Iteration 1:我必须获取ABC1 Ref set,ABC2 Ref set,ABC3 Ref set
- I want to fetch all the PackageName with respect to a ScriptName, Iteration, and ReferenceSet i.e. for Login script, Iteration 1, ReferenceSet ABC1 Ref set: I have to fetch ABC1, ABC2, ABC3
- 我想获取关于ScriptName,Iteration和ReferenceSet的所有PackageName,即对于Login脚本,Iteration 1,ReferenceSet ABC1 Ref set:我必须获取ABC1,ABC2,ABC3
Currently below is the method - getEntireCellValue() I am using to fetch the data from excel and I need help to solve the above 2 problems. Any kind of support is really appreciated.
目前下面是方法 - getEntireCellValue()我用来从excel获取数据,我需要帮助来解决上述2个问题。任何形式的支持都非常感谢。
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;
}
P.S. What I am doing here is, I am trying to fetch the number of merged rows for a script e.g. 6 rows are merged for Login script and then find number of cells inside those 6 rows to get the reference set name (3 cells). Note: When I call the above method - getNumOfMergedRows() to determine number of rows merged for Login script, I am getting 4 as output instead of 6.
附:我在这里做的是,我正在尝试获取脚本的合并行数,例如为登录脚本合并6行,然后在这6行中查找单元格数以获取参考集名称(3个单元格)。注意:当我调用上面的方法 - getNumOfMergedRows()来确定为Login脚本合并的行数时,我得到4作为输出而不是6。
2 个解决方案
#1
0
I have a sample code which takes the fiel as input.It will count the values in 3rd column for all values and stores it in a map. It does so by counting the blank values in excel file and reset the counter at each valid value
我有一个示例代码,它将fiel作为输入。它将计算所有值的第3列中的值并将其存储在地图中。它通过计算excel文件中的空白值并在每个有效值处重置计数器来实现
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Demo {
public static void main(String[] args) {
readFile(new File("excelstack.xlsx"));
}
public static void readFile(File file) {
try {
FileInputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
Map<String,Integer> map=new HashMap<String,Integer>();
int coltocheck=1;
int counter=1;
String currentitem="";
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if(currentCell.getCellTypeEnum()==CellType.BLANK ) {
if(currentCell.getColumnIndex()==coltocheck)
map.put(currentitem, counter++);
}
else if(currentCell.getCellTypeEnum()==CellType.STRING && currentCell.getColumnIndex()==coltocheck) {
currentitem=currentCell.getStringCellValue();
counter=1;
map.put(currentitem, counter++);
}
}
}
for(Map.Entry<String, Integer> m:map.entrySet())
System.out.println(m.getKey()+" "+m.getValue());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
#2
0
The below code will determine the number of merged cells in a column - colName, with begining row as startingRow
下面的代码将确定列中的合并单元格数 - colName,开始行为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;
}
#1
0
I have a sample code which takes the fiel as input.It will count the values in 3rd column for all values and stores it in a map. It does so by counting the blank values in excel file and reset the counter at each valid value
我有一个示例代码,它将fiel作为输入。它将计算所有值的第3列中的值并将其存储在地图中。它通过计算excel文件中的空白值并在每个有效值处重置计数器来实现
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Demo {
public static void main(String[] args) {
readFile(new File("excelstack.xlsx"));
}
public static void readFile(File file) {
try {
FileInputStream excelFile = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
Map<String,Integer> map=new HashMap<String,Integer>();
int coltocheck=1;
int counter=1;
String currentitem="";
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if(currentCell.getCellTypeEnum()==CellType.BLANK ) {
if(currentCell.getColumnIndex()==coltocheck)
map.put(currentitem, counter++);
}
else if(currentCell.getCellTypeEnum()==CellType.STRING && currentCell.getColumnIndex()==coltocheck) {
currentitem=currentCell.getStringCellValue();
counter=1;
map.put(currentitem, counter++);
}
}
}
for(Map.Entry<String, Integer> m:map.entrySet())
System.out.println(m.getKey()+" "+m.getValue());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
#2
0
The below code will determine the number of merged cells in a column - colName, with begining row as startingRow
下面的代码将确定列中的合并单元格数 - colName,开始行为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;
}