0% found this document useful (0 votes)
14 views6 pages

Java Excel Operations with Apache POI

The document contains code snippets for: 1) Checking if a number is a palindrome 2) Reversing a string using StringBuilder and a for loop 3) Removing duplicate characters from a string using LinkedHashSet 4) Counting the occurrences of a character in a string using a for loop 5) Code to write data to an Excel sheet and read data from an Excel sheet using Apache POI library

Uploaded by

Vishnu Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Java Excel Operations with Apache POI

The document contains code snippets for: 1) Checking if a number is a palindrome 2) Reversing a string using StringBuilder and a for loop 3) Removing duplicate characters from a string using LinkedHashSet 4) Counting the occurrences of a character in a string using a for loop 5) Code to write data to an Excel sheet and read data from an Excel sheet using Apache POI library

Uploaded by

Vishnu Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Class Palindrom

{
int r,sum=0,temp;
int n=454;

temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
s.o.p("palidrom number);
else
s,op("not palindrom);

Reverse String:
public class ReverseString()
{
public static String reverseString(String str)
{
StringBuilder sb= new StringBuilder(str);
[Link]();
return [Link]();
}}
Anothor Method:

public class ReverseString()


{
Public static void main(String args[])
{
string s="selenium";
int len=[Link]();
String rev=" ";
for (int i=len-1;i>0;i__);
{
rev=rev+charAt(i);
}
[Link](rev);
}}

Remove Duplicate Character from a String

Class RevmoceDupliacates
{

static void removeDuplicates(string str)


{
LinkedHashSet<Character> set=new LinkedHashset<>();
for (int i=0;i<[Link]();i++)
[Link]([Link](i));
for(Charachet ch:set)
S.o.p(ch);

Public static void main(String srgs[])


{
String str="removeword";
RevmoceDupliacates r= new RevmoceDupliacates();
[Link](str)
}

4.
public class CountOccurences
{
public static void main(String args[])
{

String input = "aaaabbccAAdd";


char search = 'a'; // Character to search is 'a'.

int count=0;
for(int i=0; i<[Link](); i++)
{
if([Link](i) == search)
count++;
}

[Link]("The Character '"+search+"' appears "+count+" times.");


}
}

5)Write code to write results in excel sheet.

public void Excelwrite()


{
File file=new File("D:\\[Link]");
XSSFWorkbook wb =new XSSFWoekbook();
XSSFSheet sh=[Link]();
[Link](0).createCell(0).setCellValue("Name");
[Link](0).createCell(1).setCellValue(69);

try
{
FileOutputStream fos=new FileOutputtream(file);
[Link](fos);
}
Catch(Exception e)
{
[Link]();
}

How to read the entire Excel sheet data:

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class ApachePOI {


public static void main(String args[]) throws IOException {
//Create an object of File class to open xlsx file
File file = new File("E:\\TestData\\[Link]");

//Create an object of FileInputStream class to read excel file


FileInputStream inputStream = new FileInputStream(file);

//creating workbook instance that refers to .xls file


HSSFWorkbook wb=new HSSFWorkbook(inputStream);

//creating a Sheet object


HSSFSheet sheet=[Link]("STUDENT_DATA");

//get all rows in the sheet


int rowCount=[Link]()-[Link]();

//iterate over all the row to print the data present in each cell.
for(int i=0;i<=rowCount;i++){

//get cell count in a row


int cellcount=[Link](i).getLastCellNum();

//iterate over each cell to print its value


[Link]("Row"+ i+" data is :");

for(int j=0;j<cellcount;j++){
[Link]([Link](i).getCell(j).getStringCellValue()
+",");
}
[Link]();
}
}
}

wite data in excel sheet:

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class WriteToExcel {


public static void main(String args[]) throws IOException {
//set the ChromeDriver path
[Link]("[Link]","E:\\Projects\\
[Link]");

//Create an object of File class to open xls file


File file = new File("E:\\TestData\\[Link]");

//Create an object of FileInputStream class to read excel file


FileInputStream inputStream = new FileInputStream(file);

//creating workbook instance that refers to .xls file


HSSFWorkbook wb=new HSSFWorkbook(inputStream);

//creating a Sheet object


HSSFSheet sheet=[Link]("STUDENT_DATA");

//get all rows in the sheet


int rowCount=[Link]()-[Link]();

//Creating an object of ChromeDriver


WebDriver driver = new ChromeDriver();

//Navigate to the URL


[Link]("[Link]

//Identify the WebElements for the student registration form


WebElement firstName=[Link]([Link]("firstName"));
WebElement lastName=[Link]([Link]("lastName"));
WebElement email=[Link]([Link]("userEmail"));
WebElement genderMale= [Link]([Link]("gender-radio-1"));
WebElement mobile=[Link]([Link]("userNumber"));
WebElement address=[Link]([Link]("currentAddress"));
WebElement submitBtn=[Link]([Link]("submit"));

//iterate over all the rows in Excel and put data in the form.
for(int i=1;i<=rowCount;i++) {
//Enter the values read from Excel in
firstname,lastname,mobile,email,address
[Link]([Link](i).getCell(0).getStringCellValue());
[Link]([Link](i).getCell(1).getStringCellValue());
[Link]([Link](i).getCell(2).getStringCellValue());
[Link]([Link](i).getCell(4).getStringCellValue());
[Link]([Link](i).getCell(5).getStringCellValue());

//Click on the gender radio button using javascript


JavascriptExecutor js = (JavascriptExecutor) driver;
[Link]("arguments[0].click();", genderMale);

//Click on submit button


[Link]();

//Verify the confirmation message


WebElement confirmationMessage =
[Link]([Link]("//div[text()='Thanks for submitting the form']"));

//create a new cell in the row at index 6


HSSFCell cell = [Link](i).createCell(6);

//check if confirmation message is displayed


if ([Link]()) {
// if the message is displayed , write PASS in the excel sheet
[Link]("PASS");

} else {
//if the message is not displayed , write FAIL in the excel sheet
[Link]("FAIL");
}

// Write the data back in the Excel file


FileOutputStream outputStream = new FileOutputStream("E:\\TestData\\
[Link]");
[Link](outputStream);

//close the confirmation popup


WebElement closebtn = [Link]([Link]("closeLargeModal"));
[Link]();

//wait for page to come back to registration page after close button is
clicked
[Link]().timeouts().implicitlyWait(2000, [Link]);
}

//Close the workbook


[Link]();

//Quit the driver


[Link]();
}
}
link -[Link]

read data from excel:

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class BrowserStackReadExcelTest {


public static void main (String [] args) throws IOException{
//Path of the excel file
FileInputStream fs = new FileInputStream("D:\\[Link]");
//Creating a workbook
XSSFWorkbook workbook = new XSSFWorkbook(fs);
XSSFSheet sheet = [Link](0);
Row row = [Link](0);
Cell cell = [Link](0);
[Link]([Link](0).getCell(0));
Row row1 = [Link](1);
Cell cell1 = [Link](1);
[Link]([Link](0).getCell(1));
Row row2 = [Link](1);
Cell cell2 = [Link](1);
[Link]([Link](1).getCell(0));
Row row3 = [Link](1);
Cell cell3 = [Link](1);
[Link]([Link](1).getCell(1));
//String cellval = [Link]();
//[Link](cellval);
}
}

File scr=((Takescreenshot)driver).getScreenshotAs([Link]);
[Link](scr.

You might also like