Is there a way to write list of strings into excel file using setCellData(String sheetName, String colName, int rowNum, String data)? | Selenium Forum
R
Raj Kishore Posted on 16/09/2019

Hi Ashish,

Is there a way to write list of <strings> into excel file using setCellData(String sheetName, String colName, int rowNum, String data)?

Having problem iterating colName? Can you provide an example to write a list<Strings> into excel with setCellData(String sheetName, String colName, int rowNum, String data)

 

Thank you,


R
Raj Kishore Replied on 17/09/2019

Hi Ashish,

Retrieved data is displaying vertically as you can see in the console but i need to write the appropriate data horizontally under the columns in excel sheet.

I am attaching the screenshot where you can see i am retrieving the data and displaying it console but i am not able to write it horizontally under the columns.

 

 


R
Raj Kishore Replied on 17/09/2019

Last attachment isn't working so i am attaching it in here again.


R
Raj Kishore Replied on 17/09/2019

Seems like there is a problem with the attachment. I am trying one more time to attach the screenshot

Responsive image

A
Ashish Thakur Replied on 17/09/2019

Please note the below two things

System.out.print("data to be printed");		// Keeps the cursor on same row after printing
System.out.printlln("data to be printed");	// Moves the cursor to next row after printing

These two need to be used in a combination which allows us to print the data properly.

Now, Regarding the XLS Sheet. You need to make sure that the below 4 things are provided correctly:

1. Sheet Name

2. Column Name/ Column Number

3. Row Number

4. Data

Any mistake in the above data would lead to a corrupted XLS file.


R
Raj Kishore Replied on 17/09/2019

Thanks for point the print statement.

We have only have two overloaded methods in Xls_Reader file for setCellData and those methods doesn't take 2nd parameter as int. It only takes the 2nd parameter as String colName.

1. setCellData(String sheetName,String colName,int rowNum, String data)

2. setCellData(String sheetName,String colName,int rowNum, String data, String url)

How can we iterate over the "String colName" in the written function in tableResultsSet to write the data into excel under appropriate columns.

public void tableResultsSet(String locatorKey) {
String path = "C:\\tmp\\Data.xlsx";
Xls_Reader datatable = new Xls_Reader(path);

List<WebElement> rows = getElements(locatorKey);

for (int rowNum = 0; rowNum < rows.size(); rowNum++) { // Iterate all the rows
WebElement row = rows.get(rowNum); // Get each row
List<WebElement> cells = row.findElements(By.tagName("td")); // Get each cell from each row
for (int cellNum = 0; cellNum < cells.size(); cellNum++) { // Iterate the cells in each row
String cellData = cells.get(cellNum).getText(); // Get each cell data text
System.out.print(cellData+", ");
datatable.setCellData("Sample", "ClaimNo", 2, cellData);
}
}

}

 

Responsive image

A
Ashish Thakur Replied on 17/09/2019

As I can see in the screenshots, you are providing hardcoded data.

You already have access to columnName and The data you want to add. Just provide the data there and this will work.