I have saved contact details of the same type in the same cell(e.g person has more than one email address) I have saved them as a list in one cell but I'm not able to read them when iterating through my excel sheet. I can only read the currently selected/displayed, other options are ignored. I'm using java
//Read sheet inside the workbook by its name
Sheet _workSheet = _workbook.getSheet(sheetName);
//Iterate through each rows
Iterator<Row> rowIterator = _workSheet.iterator();
while(rowIterator.hasNext())
{
//Get Each Row
Row row_ = rowIterator.next();
//Iterator through each column of each row
Iterator<Cell> cellIterator = row_.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Checking the cell format
switch(cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue()+"\n");
break;
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue()+"\n");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue()+"\n");
break;
case Cell.CELL_TYPE_BLANK:
System.err.println(cell.getStringCellValue()+" .....empty cell");
break;
}
}
System.err.println("");
}
In cell "A2" I'm expecting to get two values (emailone, emailTwo)
These did the trick;
List<XSSFDataValidation> dataValidations = sheet.getDataValidations();
Iterator<XSSFDataValidation> iterator = dataValidations.iterator();
XSSFDataValidation dataValidation = iterator.next();
String[] explicitListValues = dataValidation.getValidationConstraint().getExplicitListValues();
Related
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;
I am facing a weird problem here, i need to read the excel data which starts after some rows.
My excel input is something like below
Row1 : This report is to display all the user details
Row2 : Kindly find the below details
Row3 : TABLE1 (This is the identifier after this row my table data is available).
Row4 : ID Name DOB
Row5 : 101 RAM 10-07-1986
Row6 : 102 Sita 24-08-1989
Row6 : Table2
note:i need to read only row4 to row6jav
I need the output like below in the map,
mymap [ID =[101,102],[Name = RAM,Sita],[DOB = 10-07-1986,24-08-1989]]
I have tried the below code which is working absolutely fine if my first 3 rows are not there, only creating issue if i give first 3 rows. your help is much appreciated.
public static void main(String[] args) {
try {
File file = new File("C:\\demo\\employee.xlsx"); //creating a new file instance
FileInputStream fis = new FileInputStream(file); //obtaining bytes from the file
//creating Workbook instance that refers to .xlsx file
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0); //creating a Sheet object to retrieve object
Iterator<Row> itr = sheet.iterator(); //iterating over excel file
// CAREFUL HERE! use LinkedHashMap to guarantee the insertion order!
Map<String, List<String>> myMap = new LinkedHashMap<>();
// populate map with headers and empty list
if (itr.hasNext()) {
Row row = itr.next();
Iterator<Cell> headerIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
myMap.put(getCellValue(cell), new ArrayList<>());
}
}
Iterator<List<String>> columnsIterator;
// populate lists
while (itr.hasNext()) {
// get the list iterator every row to start from first list
columnsIterator = myMap.values().iterator();
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator(); //iterating over each column
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// here don't check hasNext() because if the file not contains problems
// the # of columns is same as # of headers
columnsIterator.next().add(getCellValue(cell));
}
}
// here your map should be filled with data as expected
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getCellValue(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING: //field that represents string cell type
return cell.getStringCellValue() + "\t\t\t";
case Cell.CELL_TYPE_NUMERIC: //field that represents number cell type
return cell.getNumericCellValue() + "\t\t\t";
case Cell.CELL_TYPE_Date: //field that represents Date cell type
return cell.getDateCellValue() + "\t\t\t";
default:
return "";
}
}
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 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.
Code:parses through the excel file using poi and prints the output in the console and also creates a new excel file to show the output.
XSSFWorkbook workbook = new XSSFWorkbook(fileName);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row ;
XSSFCell cell;
Iterator<Row> rows = sheet.rowIterator();
while(rows.hasNext())
{
row = (XSSFRow)rows.next();
Iterator<Cell> cells = row.cellIterator();
while(cells.hasNext())
{
cell = (XSSFCell)cells.next();
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()+"\t\t");
break;
case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()+ "\t\t");
break;
case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue()+ "\t\t");
break;
}
}System.out.println("");
}fileName.close();
FileOutputStream out = new FileOutputStream(new File("C://data.xlsx"));
workbook.write(out);
out.close();
Output:
Id
Name
Location
Role
Salary
111.0
Kumar
Chennai
Developer
1000.0
112.0
Larsen
Bangalore
Developer
2000.0
Queries:
1. How to get the output in the same format as in excel?
2. How to store the output in a DTO object?
try this way to add Values DTO
Create one Student DTO with properties like and setter and getters with Id,Name,Location,Role,Salary
while(rows.hasNext())
{
row = (XSSFRow)rows.next();
Iterator<Cell> cells = row.cellIterator();
StudentDTO std = new StudentDTO();
while(cells.hasNext())
{
cell = (XSSFCell)cells.next();
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()+"\t\t");
std.setId(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()+ "\t\t");
std.setName(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue()+ "\t\t");
std.setLocation(cell.getStringCellValue());
break;
}
}System.out.println("");
}