I am using Java 8.
When i am trying to access Excel data(basically this is my test data) through jdbc-odbc, i am getting "java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver"
And also i am trying to access data as non DSN.
I surfed net and came to know that Oracle deprecated support to jdbc-odbc.
So what is the easiest way to access this Excel data using Java?
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String query = "select TestScript from [TS 360 Scripts$]";
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
con = DriverManager.getConnection("jdbc:odbc:;Driver={Microsoft Excel Driver(*.xlsx)};DBQ=D://TS 360 Script with Count.xlsx");
stmt=con.createStatement();
rs=stmt.executeQuery(query);
while(rs.next())
{
System.out.println(rs.getString("TestScript"));
}
con.close();
rs.close();
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
Uday- you can easily do whatever you want to do with Apache POI jar
As Your are mentioned your requirement: of all rows having isExecuted String Yes. I tried with this jar.
Try this
package com.dd.selenium;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PerformDDTest {
private static HSSFWorkbook xlWBook;
private static HSSFSheet xlSheet;
private static HSSFRow xlRow;
private static HSSFCell xlCell;
private static String filePath = "/home/dinesh/";
private static String fileName = "test.xls";
public static void main(String[] args) throws InterruptedException {
try {
FileInputStream xlFile = new FileInputStream(filePath + fileName);
// Access the required test data sheet
xlWBook = new HSSFWorkbook(xlFile);
// Assuming your data is in Sheet1- if not use your own sheet name
xlSheet = xlWBook.getSheet("Sheet1");
// gives row count in sheet
int noOfRows = xlSheet.getPhysicalNumberOfRows();
// gives column count in sheet
xlRow = xlSheet.getRow(0);
int noOfColumns = xlRow.getLastCellNum();
// excelData - 2 dimm array - stores all the excel data -Sheet1 only
String[][] excelData = new String[noOfRows][noOfColumns];
// r - row c- column
for (int r = 1; r < noOfRows; r++) {
for (int c = 0; c < noOfColumns; c++) {
xlRow = xlSheet.getRow(r);
xlCell = xlRow.getCell(c);
// Here we have complete excel data in an array -excelData-
excelData[r][c] = xlCell.getStringCellValue();
// System.out.println("row: " + r + " column: " + c);
// System.out.println(excelData[r][c]);
}
}
// creating an array to store isExected column
String[][] isExecuted = new String[noOfRows][1];
for (int row = 1; row < noOfRows; row++) {
// here column is always only one
// so c=0
// extracting a isExecuted column - and considering it as last
// column in sheet
// in your case it is not then - count the column position : use
// position-1
// ex: if column position is 7 then use 6 as below
// isExecuted[row][0]= excelData[row][6];
isExecuted[row][0] = excelData[row][noOfColumns - 1];
if (isExecuted[row][0].equalsIgnoreCase("yes")) {
// accessing complete row -which isExecuted=Yes
// *********IMPORTANT*****
for (int col = 0; col < noOfColumns; col++) {
// prints all the rows where isExecuted column has Yes
System.out.println(excelData[row][col]);
}
}
// System.out.println(isExecuted[row][0]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I used this Excel Data:
Test Case Name Username Password Results IsExecute
APACHE_POI_TC testuser_1 Test#123 Pass Yes
APACHE_POI_TC testuser_2 Test#124 Pass No
APACHE_POI_TC testuser_3 Test#125 Pass Yes
APACHE_POI_TC testuser_4 Test#126 Pass Yes
APACHE_POI_TC testuser_5 Test#127 Pass No
APACHE_POI_TC testuser_6 Test#128 Pass Yes
Dont Access Excel file as a database. Instead use a jar such
as Apache POI For Microsoft Documents
Download Link: Apache POI For MS Docs- Jar
An Example For using this API:
Note: you must add apache poi jar to your build path before running it
package com.dd.selenium;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PerformDDTest {
private static HSSFWorkbook xlWBook;
private static HSSFSheet xlSheet;
private static HSSFRow xlRow;
private static HSSFCell xlCell;
private static String filePath = "/home/dinesh/";
private static String fileName = "test.xls";
private static String url = "http://store.demoqa.com/";
private static String result = "Pass";
public static void main(String[] args) throws InterruptedException {
try {
FileInputStream xlFile =
new FileInputStream(filePath+fileName);
//Access the required test data sheet
xlWBook = new HSSFWorkbook(xlFile);
xlSheet = xlWBook.getSheet("Sheet1");
xlRow = xlSheet.getRow(1);
String username = xlRow.getCell(1).getStringCellValue();
String password = xlRow.getCell(2).getStringCellValue();
FirefoxDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(url);
driver.findElement(By.xpath(".//*[#id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys(username);
driver.findElement(By.id("pwd")).sendKeys(password);
driver.findElement(By.id("login")).click();
driver.findElement(By.xpath(".//*[#id='account_logout']/a")).click();
Thread.sleep(5000);
driver.quit();
setResultCell();
FileOutputStream fout = new FileOutputStream(filePath+fileName);
xlWBook.write(fout);
fout.flush();
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
result = "Failed";
setResultCell();
e.printStackTrace();
}
}
private static void setResultCell() {
xlCell = xlRow.getCell(3, xlRow.RETURN_BLANK_AS_NULL);
if(xlCell == null ){
xlCell = xlRow.createCell(3);
xlCell.setCellValue(result);
}else{
xlCell.setCellValue(result);
}
}
}
This might be a little late but in case you still have the issue you can retain access excel as a db with java 8 using Fillo: http://codoid.com/fillo/
Related
I am scraping data from a table using selenium with Java but is slow and I am not sure why. Is there a reason why and how can I speed it up? The other thing I noticed is that it seems to slow down more as it progresses. I noticed this by observing the print statements to the console.
Here is my code:
package mypackage;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import com.seleniumpractice.utilities.XLUtils;
import io.github.bonigarcia.wdm.WebDriverManager;
public class CovidWebTable {
static WebDriver driver;
static XLUtils xl;
static List<WebElement> header;
static List<WebElement> rows;
public static void main(String[] args) throws IOException {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("https://www.worldometers.info/coronavirus");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
WebElement table = driver.findElement(By.xpath("//table[#id='main_table_countries_today']"));
rows = table.findElements(By.xpath(".//tr[#role='row']"));
System.out.println("Total rows: "+rows.size());
xl = new XLUtils(".\\datafiles\\covid.xls");
//xl.setCellData(null, rows, rows, null);
//Add header
header = table.findElements(By.xpath(".//thead//th"));
System.out.println("Header cols: "+ header.size());
for(int col=1; col<header.size()-1; col++) {
xl.setCellData("Covid Data", 0, col-1, header.get(col).getText());
}
int xlRow = 1;
for(int r=1; r<rows.size(); r++) {
String a = rows.get(r).getText();
if(rows.get(r).getText().equals("")) {
System.out.println("Skipped row: "+r);
continue;
}
System.out.println("Writing row "+r);
for(int c=1; c<header.size(); c++) {
//String data = rows.get(r).findElement(By.xpath(".//td["+(c+1)+"]")).getText();
xl.setCellData("Covid Data", xlRow, c-1, rows.get(r).findElement(By.xpath(".//td["+(c+1)+"]")).getText());
}
xlRow++;
}
System.out.println("Complete.");
driver.close();
}
}
The code that contains the code for writing to excel:
package com.internetBanking.utilities;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
public class XLUtils {
public static FileInputStream fi;
public static FileOutputStream fo;
public static HSSFWorkbook wb;
public static HSSFSheet ws;
public static HSSFRow row;
public static HSSFCell cell;
public static int getRowCount(String xlfile, String xlsheet) throws IOException {
fi = new FileInputStream(xlfile);
wb = new HSSFWorkbook(fi);
ws = wb.getSheet(xlsheet);
int rowcount = ws.getLastRowNum();
wb.close();
fi.close();
return rowcount;
}
public static int getCellCount(String xlFile, String xlSheet, int rowNum) throws IOException {
fi = new FileInputStream(xlFile);
wb = new HSSFWorkbook(fi);
ws = wb.getSheet(xlSheet);
row = ws.getRow(rowNum);
int cellCount = row.getLastCellNum();
wb.close();
fi.close();
return cellCount;
}
public static String getCellData(String xlFile, String xlSheet, int rowNum, int colNum) throws IOException {
fi = new FileInputStream(xlFile);
wb = new HSSFWorkbook(fi);
ws = wb.getSheet(xlSheet);
row = ws.getRow(rowNum);
cell = row.getCell(colNum);
String data;
try {
String cellData = new DataFormatter().formatCellValue(cell);
return cellData;
}
catch(Exception e) {
data = "";
}
wb.close();
fi.close();
return data;
}
public static void setCellData(String xlFile, String xlSheet, int rowNum, int colNum, String data) throws IOException{
fi = new FileInputStream(xlFile);
wb = new HSSFWorkbook(fi);
ws = wb.getSheet(xlSheet);
row = ws.getRow(rowNum);
Cell cell = row.createCell(colNum);
cell.setCellValue(data);
//cell = row.getCell(colNum);
//cell.setCellValue(data);
fo = new FileOutputStream(xlFile);
wb.write(fo);
wb.close();
fi.close();
fo.close();
}
}
Ok getting to understand the code will give you a hint of what you should do, the code is iterating 231 read data from the table and write it to an excel file (daaa!) Ok but when writing into the excel file you write row by row (so!) You then iterate cell by cell on each row on each cell you call setCellData(...) in that XLUtils and here is when it starts to slow down!
In that setCellData each time you call it, it read a file from the disk, opens it, appends data then close the file and since you call it by cell, you end up calling it ~231 (rows) x 15 (col) = 3465
Imagine the time consumed (opening file/writing data/ closing file) 3465 times
So what to do,
You need to create a list of list
List<ArrayList> rows = new ArrayList()
This is a list of list ^ where each record in the list is another list
and on each row you read from the table (even headers), you create a list of cells and then you add this list to the rows list!
and eventually, you add some utility function in that XLUtils that accept List<ArrayList> that will
Open the file once, Iterate on that rows list write their values, and close the file.
Imagine it as if you are trying to move from house to house, and you were using your personal small pickup truck car to move stuff. It won't take all the house stuff so you would go back and forth for a while. On the other hand, you could have used a furniture truck that picks up all furniture and loads it at once.
I am trying to read the following data from an Excel sheet
With the following code
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public String readUsernameFromExcel() {
File src = new File("C:/filepath.xls");
try {
Workbook wb = Workbook.getWorkbook(src);
Sheet sh1 = wb.getSheet(0);
Cell a2 = sh1.getCell(0, 2);
data1 = a2.getContents().trim();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data1;
}
So when I try and get the cell 0,1 I can pick up the username 1000483 just fine. But when I try to read 0,2 and I get java.lang.ArrayIndexOutOfBoundsException: 2.
What I'm trying to do is read data from an excel sheet return it as a String and then pass it in to login my application. But it seems when I try 0,2 I'm going outside of what is expected. I've tried a few things such as a for loop
for (int rows = 0; rows < sh1.getRows(); rows++) {
Sheet sh1 = wb.getSheet(0);
Cell a2 = sh1.getCell(0, 2);
}
I understand the first number is the column and the second is the row. I also understand that the code isn't able to see past 0,1. I'm just at a loss as to how to get it to see the rest of the sheet after trying other solutions of the same problem.
sh1.getRows() returns 3. As loop starts from 0, sh1.getRows() needs to be decremented by 1 (as below). Below loop works fine and returns value properly.
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class Excel {
public static void main(String[] args) {
File src = new File("c:/filepath.xls");
try {
String data1;
Workbook wb = Workbook.getWorkbook(src);
Sheet sh1 = wb.getSheet(0);
for (int rows = 1; rows < sh1.getRows(); rows++) {
for (int column = 0; column <= sh1.getColumns()-1; column++) {
Cell a2 = sh1.getCell(column, rows);
data1 = a2.getContents().trim();
System.out.println(data1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The above code works and fetches the date without error
I use the same data with you, and I could get 1000484 value through my code.
Here is my code :
package com.jason.xls;
import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class XlsParser {
public static void main(String[] args) {
final String path = "/home/coder/filepath.xls";
System.out.println(readUserNameFromXls(path));
}
public static String readUserNameFromXls(final String path) {
File file = new File(path);
try {
Workbook wb = Workbook.getWorkbook(file);
Sheet sheet = wb.getSheet(0);
Cell a2 = sheet.getCell(0, 2);
return a2.getContents().trim();
} catch (Exception e) {
return null;
}
}
}
I download jxl.jar from jxl.jar download here
My code result is : Code Result Image
I am passing in parameters from excel sheet using TestNg dataprovider and Apache POI api.
The trouble I am having is passing in a numeric value. My code:
#Test(dataProvider = "RegisterPage")
public void registerPage(String foreName, String surName, int dateOfBirth, String addressLine1, String addressLine2, String addressLine3, String City,
String County, String postCode, String nationalInsuranceNO, int telephoneNo, String userName, String confirmUsername, String password,
String confirmPassword, String memorableWord){
The error I get from console is: Cannot get a text value from a numeric cell
ExcelUtils Class File Code:
package testNG;
//Excel Function to open, read and write parameters/values from an Excel Document to class files or tests.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;
public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception {
String[][] tabArray = null;
try {
FileInputStream ExcelFile = new FileInputStream("C://Selenium-java-maven//workSpace//SeleniumWebDriver-TestNG//src//regData//RegisterOLPTestData.xlsx");
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int startRow = 1; // Starts from row 1 in Excel not row 0
int startCol = 1; // Starts from Columns 1 in Excel not row 0
int ci,cj;
int totalRows = ExcelWSheet.getLastRowNum();
// you can write a function as well to get Column count
int totalCols = 3; //Total Sheet columns
tabArray=new String[totalRows][totalCols];
ci=0;
for (int i=startRow;i<=totalRows;i++, ci++) {
cj=0;
for (int j=startCol;j<=totalCols;j++, cj++){
tabArray[ci][cj]=getCellData(i,j);
System.out.println(tabArray[ci][cj]);
}
}
}
catch (FileNotFoundException e){
System.out.println("Could not read the Excel sheet");
e.printStackTrace();
}
catch (IOException e){
System.out.println("Could not read the Excel sheet");
e.printStackTrace();
}
return(tabArray);
}
public static String getCellData(int RowNum, int ColNum) throws Exception {
try{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
int dataType = Cell.getCellType();
if (dataType == 3) {
return "";
}else{
String CellData = Cell.getStringCellValue();
return CellData;
}}catch (Exception e){
System.out.println(e.getMessage());
throw (e);
}
}
}
The problem is that you try to pass a String as an int. If you have a method, which expects an integer, but you pass a String, then you will receive this error. You either change the type of the parameter, overload the method or pass Integer.parseInt(foo) instead of foo.
EDIT:
Finally got more information about the problem. The problem is that the parameter dateOfBirth is int and a String is passed. The simple solution would be to change the type of dateOfBirth to String, so there will be no type conflict at the end. Depending on the needs, it might be better to use a class designed specifically for this purpose, like Date, both in the method and at the place where the method is called.
I am new to selenium webdriver. I am trying to retrieve the values from excel sheet for dropdownlist and checkboxes/radio button and use those values in selenium webdriver. Can you please give some suggestions for how to retrieve those values?
Thanks in advance.
//download jxl.jar and put it in your build path
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
//******************************************************************************************
public String setWorkbook(Workbook workbook) {
String sFullPathNameExcelFile = getXmlConfig().getString("data.input_excel");
// hard coded:
// String sFullPathNameExcelFile = "C:\\user\\MyExcel.xls";
try {
this.workbook = Workbook.getWorkbook(new File(
sFullPathNameExcelFile));
} catch (Exception ex) {
System.out.println("******* ERROR *******\n******* ERROR *******\n******* ERROR *******");
System.out.println(sFullPathNameExcelFile+" DOES NOT EXIST OR CANNOT BE OPENED FOR INPUT.");
System.out.println("******* ERROR *******\n******* ERROR *******\n******* ERROR *******");
System.exit(0);
}
return sFullPathNameExcelFile;
}// end method
//*****************************************************************************
public String setSheet(Sheet sheet) {
String sWorksheet = getXmlConfig().getString(
"data.worksheet");
try{
this.sheet = workbook.getSheet(sWorksheet);}
catch (Exception ex) {return "ERROR";}
return sWorksheet;
}
//******************************************************************************
public Sheet getSheet() {
return sheet;
}
public static void main(String[] args) throws Exception {
Workbook workbook = null;
pca.sWorkingExcelFile = pca.setWorkbook(workbook);
int iCol = 27; //whatever you need
int iRow = 34; //whatever you need
Cell cCell = null;
Sheet sheet = pca.getSheet();
cCell = sheet.getCell(iCol,iRow);
String str = cCell.getContents();
}
I am currently working on a project for my computer science class, and I was trying to figure out how to retrieve and print certain values from a file in excel. Such as, how would I go about printing the integer in column J, row 6?
Better yet, is there a way for me to return the row number of a string in column 1? Such as, if I had a string "Phone" in column 1, could I use a command to return the row number of the first instance of "phone"?
I have looked at other questions, none of which sufficiently answered my own.
Here you go refer to this class file for iterating over an excel file
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
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 Test {
private static final String FILE_NAME = "/users/developer/Documents/myFile.xlsx";
public void employeesUpload() {
String fName = "";
String lName = "";
String phoneNumber = "";
String email = "";
String gender = "";
String employeeCode = "";
try {
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
int rowIndex = 0;
DataFormatter formatter = new DataFormatter();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
if (rowIndex > 0) {
Iterator<Cell> cellIterator = currentRow.iterator();
employeeCode = fName = lName = phoneNumber = email = gender = "";
int cellIndex = 0;
while (cellIndex <= 5) {
Cell currentCell = currentRow.getCell(cellIndex);
if (cellIndex == 4) {
employeeCode = formatter.formatCellValue(currentCell).trim();
}
if (cellIndex == 1) {
fName = formatter.formatCellValue(currentCell).trim();
}
if (cellIndex == 2) {
lName = formatter.formatCellValue(currentCell).trim();
}
if (cellIndex == 0) {
email = formatter.formatCellValue(currentCell);
email = email.trim().toLowerCase();
}
if (cellIndex == 3) {
phoneNumber = formatter.formatCellValue(currentCell).trim();
}
cellIndex++;
}
Cell resultCell = currentRow.getCell(7);
if (resultCell == null) {
resultCell = currentRow.createCell(7);
}
Cell employementIdCell = currentRow.getCell(8);
if (employementIdCell == null) {
employementIdCell = currentRow.createCell(8);
}
if (true) {
resultCell.setCellType(Cell.CELL_TYPE_STRING);
employementIdCell.setCellValue("Success");
resultCell.setCellValue(email);
} else {
resultCell.setCellType(Cell.CELL_TYPE_STRING);
resultCell.setCellValue("Error");
}
}
rowIndex++;
}
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws ParseException, UnsupportedEncodingException {
Test employeesBulkUpload = new Test();
employeesBulkUpload.employeesUpload();
}
}
Hope this helps :)
user https://github.com/jueyue/easypoi this jar
use annotion to easy read excel
public class ExcelImportNewDateTest {
#Test
public void importTest() {
ImportParams params = new ImportParams();
params.setTitleRows(1);
params.setHeadRows(1);
long start = new Date().getTime();
List<NewDateEntity> list = ExcelImportUtil.importExcel(
new File(FileUtilTest.getWebRootPath("import/ExcelNewDateTest.xlsx")), NewDateEntity.class, params);
System.out.println(new Date().getTime() - start);
Assert.assertEquals(list.size(), 100);
System.out.println(list.size());
System.out.println(ReflectionToStringBuilder.toString(list.get(1)));
}
}