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 17, 2019 in Selenium by Prakash
3,831 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 17, 2019 by Anvi
• 14,150 points

Related Questions In Selenium

0 votes
1 answer

How can I read numeric data from an Excel sheet using Selenium Webdriver?

Hey Jignesh, for reading numeric data from ...READ MORE

answered Jul 17, 2019 in Selenium by Abha
• 28,140 points
8,192 views
0 votes
1 answer

How can we read data from an excel sheet in Selenium webdriver?

Hi Tarun, to read data from an ...READ MORE

answered May 8, 2019 in Selenium by Abha
• 28,140 points
17,578 views
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, 2019 in Selenium by Deeksha
681 views
0 votes
2 answers

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,500 points
13,674 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,143 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,875 views
0 votes
1 answer
0 votes
1 answer

How can I select an option from a dropdown using Ruby Selenium Webdriver?

Hey Swasti, you can try following lines ...READ MORE

answered Aug 26, 2019 in Selenium by Anvi
• 14,150 points
4,763 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP