Can anyone show how to modify an existing excel sheet using Apache POI?

0 votes
Can anyone show how to modify an existing excel sheet using Apache POI?
Jul 16 in Selenium by Prakash
51 views

1 answer to this question.

0 votes

Hello Prakash, to modify an existing excel sheet using Apache POI, checkout this code. Here we write some text to 3rd column of the sheet:

public class WriteToExcel{
  WebDriver driver;
  WebDriverWait wait;
  HSSFWorkbook workbook;
  HSSFSheet sheet;
  HSSFCell cell;
        
  public void ReadData() throws IOException
  {
	System.setProperty("webdriver.gecko.driver", "C:\\Users\\geckodriver.exe");
	driver = new FirefoxDriver();
	driver.get("http://www.linkedin.com/");
	driver.manage().window().maximize();
	wait = new WebDriverWait(driver,30);
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 
	File src=new File("C:\\Users\\Admin\\Desktop\\TestData.xls");
	FileInputStream finput = new FileInputStream(src);
        workbook = new HSSFWorkbook(finput);
	 
	sheet= workbook.getSheetAt(0);
	 
	for(int i=1; i<=sheet.getLastRowNum(); i++){
		 // Import data for Email.
		 cell = sheet.getRow(i).getCell(1);
		 cell.setCellType(Cell.CELL_TYPE_STRING);
		 driver.findElement(By.id("login-email")).sendKeys(cell.getStringCellValue());
		 
		 // Import data for password.
		 cell = sheet.getRow(i).getCell(2);
		 cell.setCellType(Cell.CELL_TYPE_STRING);
		 driver.findElement(By.id("login-password")).sendKeys(cell.getStringCellValue());
		 
		 // Write data in the excel.
	   FileOutputStream foutput=new FileOutputStream(src);
		
		// Specify the message needs to be written.
		String message = "Data Imported Successfully.";
		
		// Create cell where data needs to be written.
		sheet.getRow(i).createCell(3).setCellValue(message);
		 
		// Specify the file in which data needs to be written.
	    FileOutputStream fileOutput = new FileOutputStream(src);
	    
	    // finally write content
	    workbook.write(fileOutput);
		
	     // close the file
	    fileOutput.close();
	    	
	 }
 } 
}
answered Jul 16 by Anvi
• 13,960 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
0 answers

How to implement Data-Driven framework using Apache POI?

How to implement Data-Driven framework using Apache ...READ MORE

Jul 15 in Selenium by Deeksha
48 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
1,126 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,150 points
1,239 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,150 points
988 views
0 votes
1 answer