I have excel file having the below data, and i am trying to fetch the data through data provider method.
The scenario here is - if the value of "To Be Executed" is "Yes" then that respective testcase scenario needs to be run.
But i am facing problem here. Find the snippet of excel file and code.
Any help would be greatly appreciated.
public Boolean getDataFromExcel(String excelName, String sheetName) {
wb = getExcelWorkbook(excelName);
Sheet sheet = wb.getSheetAt(0);
//Row firstRow = sheet.getRow(0);
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell.getStringCellValue().equalsIgnoreCase("Yes")){
return true;
}
else
return false;
Related
I use the following approach to read data from excel (csv) sheet:
InputStream inputStream = new BufferedInputStream(multipartFile.getInputStream());
XSSFWorkbook myWorkBook = new XSSFWorkbook(inputStream);
List<String> cellList = new ArrayList<>();
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator<Row> rowIterator = mySheet.iterator();
rowIterator.next();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
cellList.clear();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) { // this returns false even the cell has data
Cell cell = cellIterator.next();
cellList.add(cell.getStringCellValue());
}
}
I want to make cellIterator.hasNext() returns true for empty cell values as in the second row (name). So, how can I do that?
Create Date Uuid Name
2021-09-29 2e81a2b6-3226-4fe0-b8bd-39f42239686d admin
2021-09-30 610e9040-840c-48c5-aa64-f193ed896133
You can't make cellIterator.hasNext() return true for the empty "name" cell.
If you know in advance the number of column in advance, you can use a loop and row.getCell(index).
for (int i = 0; i < 3; i++) {
Cell cell = row.getCell(i);
cellList.add(cell == null ? "Empty":cell.toString());
}
If you don't, you have to scan first of your file to count the number of columns.
I am trying to get the values from cells in an excel spreadsheet that contain both letters and numbers. Below is a sample of my code:
String excelFilePath = "FILEPATH";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
iterator.next();
while (rowIterator.hasNext()) {
Row nextRow = rowIterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell nextCell = cellIterator.next();
int columnIndex = nextCell.getColumnIndex();
switch (columnIndex) {
case 0:
String name = nextCell.getStringCellValue();
break;
case 1:
Date enrollDate = nextCell.getDateCellValue();
case 2:
String userid = nextCell.getStringCellValue();
}
}
My question is in relation to case 2.User IDs can either be letters, numbers or a mix of both for example: (AX77552, 112321, ASWTT)If I say getStringCellValue() I get an error and I also get an error if I say getNumericCellValue(). Is there any way that I can make it so that you can get numeric and string values for the cells in this row of data?
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
}
I have a row iterator that goes through excel sheet and gets every cell value in that row. I have successfully printed the values out, row by row, but I'd now like to store each cell in a row in separate variables so that I can pass them to Application Module where they will be used as parameters in an insert function. How to store cell value in variables ? Thanks. Here is the code:
try {
InputStream is = file.getInputStream();
HSSFWorkbook workbook = new HSSFWorkbook(is);
HSSFSheet sheet = workbook.getSheetAt(0);
System.out.print("File is up and the size is " + file.getLength() + " bytes\n");
Iterator<Row> rowIterator = sheet.iterator();
if (rowIterator.hasNext())
rowIterator.next();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
// System.out.print(cell.getNumericCellValue()+ "\t");
break;
}
}
System.out.println("");
}
// workbook.close();
// file.close();
}
catch (IOException e) {
System.out.print("greška");
}
For anyone wondering how to do this. Iterate through every row and store it in an arrayList, and then store every ArrayList into another ArrayList. It should look like this ArrayList>. After you get that object send it to your Application module where you "unpack" your ArrayList with an enhanced for loop. After that extract elements from the array into variables and insert them in your view object.
I am trying to read excel file data and saving that data into the database.
data is comming to console. I have two column in excel file namely Userid and RequestId I am trying to save the data similarly in the database table in the same manner.
Data is going in the table but both columns are having same value.
I need them seperately.I am using this code for reading.
FileInputStream file2 = new FileInputStream(new File("C://excel.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file2);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
double value2=(int) cell.getNumericCellValue();
System.out.println("value1: "+value2);
}`
In console it is showing correctly but the data is not going in that manner in database
Please help me
Thanks in advance
With some Data class:
class Data {
private final double val1;
private final double val2;
public Data(final double val1; final double val2) {
this.val1 = val1;
this.val2 = val2;
}
// getters etc
}
and
final List<Data> datas = new ArrayList<>();
for (final Row r : sheet) {
// Maybe skip an header row
final Cell cell1 = r.getCell(0); // 0-based
final Cell cell2 = r.getCell(1);
if (cell1 != null && cell2 != null) {
datas.add(new Data(cell1.getNumericCellValue(), cell2.getNumericCellValue()));
}
}
// TODO: write datas to DB
NB: wild code (i.e. didn't even try to compile it)
Some reading: https://poi.apache.org/spreadsheet/quick-guide.html#Iterator