Showing posts with label Selenium-Excel. Show all posts
Showing posts with label Selenium-Excel. Show all posts

Tuesday, March 15, 2016

Excel :Create Drop Down in Excel Sheet(Test Data) with colored font

Create Drop Down in Test Data with colored font


To add this drop-down list to a sheet, do the following:
  1. Create the list in cells A1:A4. ...
  2. Select cell E3. ...
  3. Choose Validation from the Data menu.
  4. Choose List from the Allow option's drop-down list. ...
  5. Click the Source control and drag to highlight the cells A1:A4. ...
  6. Make sure the In-Cell Dropdown option is checked. ...
  7. Click OK.
Color coding a drop-down list in Excel's 
    1) Create your drop down list in any cell using Data Validation with fields. e.g, Low, Medium,High
    2) Highlight the drop down cell.
    3) Select Conditional Formatting
    4) Select Highlight cell rules, more rules
    5) Select Format only cells that contain
    6) Change the value in the format only cells with: to Specific Text
    7) Enter the text field . e.g. Low
    Select Format tab.
    9) Select the Fill tab and select your colour e.g. Green.
    10) Click Ok

Sunday, June 29, 2014

Selenium : Read ,Write and Edit Excel Sheet (Spread Sheet)

Excel Selenium : Read ,Write and Edit Excel Sheet (Spreadsheet)-- Final

Download the latest binary zip file below link and jars into your project
http://poi.apache.org/download.html


Code below(unlike jxl this should work for both xls and xlsx files) :

Workbook wb = WorkbookFactory.create(new FileInputStream("C:\\Users\\DPK\\Desktop\\test.xls"));
Sheet sheet = wb.getSheet("Sheet1");

System.out.println(sheet.getRow(0).getCell(0).getRichStringCellValue()); // Read Cell
sheet.createRow(1).createCell(1).setCellValue(1212); //---Write into a new cell
sheet.getRow(0).getCell(0).setCellValue("333"); //----Edit existing Cell
FileOutputStream fileOut = new FileOutputStream("C:\\Users\\DPK\\Desktop\\test.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("ok");

Wednesday, September 11, 2013

Selenium : Fetch Data from Excel using Query (Better Version)

Fetch Data from Excel using Query  (Better Version)

Link to download JAR files for Apache POI : http://poi.apache.org/download.html#POI-3.9 and http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.9-20121203.zip (might change according to the new update ).

Configuration :
http://catchbug.blogspot.in/2013/04/selenium-connect-to-excel-spreadsheet.html



public class Sample1
{
public static void main(String[] args)
{
Object oData=Excel.DB_Read("Datasheet", "Select Label,Xpath from [Sheet1$] where Module='Module 1'");
System.out.println(oData);
oData.toString();
}
}

class Excel{
public static Object DB_Read(String sWb,String sQuery)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:"+sWb);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sQuery);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount(); //Get Column Count

Object columnValue ="";// null;
while (rs.next())
{
for (int i = 1; i <= numberOfColumns; i++)
{
columnValue = columnValue+rs.getString(i);
if (i != numberOfColumns)
columnValue = columnValue+"|";
}
columnValue=columnValue+"\n";
}
st.close();
con.close();
return columnValue;
}
catch (Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
return null;
}
}
}

Sunday, June 16, 2013

Selenium : Create Excel ,Read and Write Excel into a Array

Selenium : Create Excel ,Read and Write Excel into a Array


Link to download JAR files for Apache POI : http://poi.apache.org/download.html#POI-3.9 and http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.9-20121203.zip (might change according to the new update ).

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.WorkbookFactory;


public class Excel_Operations
{

public static void main(String[] args)
{
System.out.print(Excel_Operations.create_Excel("C:\\Created_with_selenium.xls","D1"));
System.out.print(Excel_Operations.read_excel("C:\\Created_with_selenium.xls","D1"));
String sData[][]=new String[1][1];
sData[0][0]="00";
//sData[0][1]=1;
//sData[1][0]=2;
//sData[1][1]=3;
System.out.print(Excel_Operations.write_excel("C:\\Created_with_selenium.xls", "D1", sData));
}
//-------------------------------------------------------------------------------------
public static boolean create_Excel(String sPath,String sSheet_name)
{

Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
Sheet sheet1 = wb.createSheet(sSheet_name);
try{
FileOutputStream fileOut = new FileOutputStream(sPath);
wb.write(fileOut);
fileOut.close();
return true;
}catch(Exception E)
{
System.out.print("could not create an excel file");
return false;
}

}
//---------------------------------------------------------------------------

public static String read_excel(String sPath,String sSheet_name)
{
String sData="";
try
{
InputStream oFile = new FileInputStream(sPath);
Workbook wb = WorkbookFactory.create(oFile);
Sheet sheet = wb.getSheet(sSheet_name);//.getSheet("Sheet1");

for (Row row : sheet) //Row Count=sheet.getLastRowNum();
{
for (Cell cell : row) //Column Count= row.getLastCellNum();
{
switch (cell.getCellType())//1- string
{
case 0://cell.CELL_TYPE_NUMERIC:
sData=sData+cell.getNumericCellValue();
break;
case 1://cell.CELL_TYPE_STRING:
sData=sData+cell.getStringCellValue();
break;
}
sData=sData+"|";
}
sData=sData+"\n";
}
}catch(Exception e)
{
System.out.print("File not found");
}
return sData;
}
//-------------------------------------------------------------------------------
public static boolean write_excel(String sPath,String sSheet_name,String oData[][])
{
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet(sSheet_name);
int iRows=oData.length;
int iCols=oData[0].length;
for (int iR_count=0;iR_count<=iRows-1;iR_count++ )
{
Row row = sheet.createRow(iR_count);
for (int iC_count=0;iC_count<=iCols-1;iC_count++)
{
row.createCell(iC_count).setCellValue(oData[iR_count][iC_count]);
}
}

try{
FileOutputStream fileOut = new FileOutputStream(sPath);
wb.write(fileOut);
fileOut.close();
}catch (Exception e)
{
System.out.print("file not created ");
return false;
}
return true;
}

}

Selenium : Excel operations

Selenium : Excel operations 

Please visit http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook  for more details 

Link to download JAR files for Apache POI : http://poi.apache.org/download.html#POI-3.9 and http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.9-20121203.zip (might change according to the new update ).

//New Workbook
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
//New Sheet
Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("second sheet");

// Note that sheet name is Excel must not exceed 31 characters
// and must not contain any of the any of the following characters:
// 0x0000
// 0x0003
// colon (:)
// backslash (\)
// asterisk (*)
// question mark (?)
// forward slash (/)
// opening square bracket ([)
// closing square bracket (])

// You can use org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
// for a safe way to create valid names, this utility replaces invalid characters with a space (' ')
String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns " O'Brien's sales "
Sheet sheet3 = wb.createSheet(safeName);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

//Working with different types of cells
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short)2);
row.createCell(0).setCellValue(1.1);
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue(Calendar.getInstance());
row.createCell(3).setCellValue("a string");
row.createCell(4).setCellValue(true);
row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
//Iterate over rows and cells
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
// Do something here
}
}
//Getting the cell contents
To get the contents of a cell, you first need to know what kind of cell it is
(asking a string cell for its numeric contents will get you a
NumberFormatException for example). So, you will want to switch on the cells
type, and then call the appropriate getter for that cell.
In the code below, we loop over every cell in one sheet, print out the cells
reference (eg A3), and then the cells contents.

// import org.apache.poi.ss.usermodel.*;

Sheet sheet1 = wb.getSheetAt(0);
for (Row row : sheet1) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
System.out.print(cellRef.formatAsString());
System.out.print(" - ");

switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println();
}
}
}
//Reading and Rewriting Workbooks
InputStream inp = new FileInputStream("workbook.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");

Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

Saturday, April 13, 2013

Selenium :Connect to an Excel Spreadsheet using JDBC in Java


Connect to an Excel Spreadsheet using JDBC in Java



This tutorial will show you how to connect to and read data from an Excel spreadsheet using JDBC.

To start, we need to setup the local ODBC connection.

Navigate to your computers Control Panel and locate the Administrative Tools.



Once in the Administrative Tools section, locate Data Sources (ODBC)



The ODBC Data Source Administor menu will open



Select the System DSN tab and click Add

Find Driver do Microsoft Excel(*.xls) from the list and click Finish

Give the Data Source Name & Description



Next, click Select Workbook and locate the spreadsheet you wish to use



In this case, we are using worcester.xls. Select it and click OK.

Click OK again to exit the setup. The ODBC connection is now complete.

Now that the ODBC connection is setup, its time for the Java code.


Note :1st Row in Excel is considered as column header 

-------------------------------------------------------------------
import java.sql.*;
public class Connect_Xl_using_JDBC
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:worcester");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from [Sheet1$]");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
System.out.println("Columns="+numberOfColumns);

while (rs.next())
{
for (int i = 1; i <= numberOfColumns; i++)
{
if (i > 1)
System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);

}
System.out.println("");
}
st.close();
con.close();
}
catch (Exception ex)
{
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
}

------------------------------------------------------



Friday, February 22, 2013

Selenium : Apache POI ,xls vs xlsx


xls vs xlsx(http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook)


// Use a file
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));

// Use an InputStream, needs more memory
Workbook wb = WorkbookFactory.create(new FileInputStream("MyExcel.xlsx"));

Tuesday, February 19, 2013

Selenium: To display all the Rows and Columns in an excel sheet

To display all the rows and columns inside Spreadsheet

//All the related API and JARS should be added (Read previous program ) for more details




import
java.io.File;
import
java.io.IOException;
//import java.util.Locale;
//import javax.swing.JOptionPane;
import
jxl.*;
import
jxl.read.biff.BiffException;
//import jxl.write.*;
 
public
class Framework
{
public static void main(String[] args) throws BiffException, IOException
{
String sPath=JOptionPane.showInputDialog("Enter excel Path");//"D:\\Selenium\\sample.xls"
Workbook workbook = Workbook.getWorkbook(
new File ("sPath"));//
Sheet sheet = workbook.getSheet(0); //workbook.getSheet("Sheet1")
for (int i = 0; i < sheet.getRows(); i++)
{
for (int j = 0; j < sheet.getColumns(); j++)
{
Cell cell = sheet.getCell(j,i);
//getcell(col,row)
System.
out.print(cell.getContents()+" ");
}
System.
out.println("");// or System.out.print("\n");
}
workbook.close();
}
}