• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to Read and Write excel files in Selenium using Apache POI

How to Read and Write excel files in Selenium using Apache POI

November 1, 2016 by Mukesh Otwani 160 Comments

Excel-Reading in Selenium

Hello, Welcome to Selenium tutorial, in this post, we will see how to Read and Write excel files in Selenium

Selenium support only Web browser automation so for Read and Write excel files in Selenium we have to take help of third party API like JExcel and Apache POI

Apache POI is an API, which is freeware and written in Java and gives so much flexibility to read/write files it has so many predefined methods, classes, and interfaces.

Once you move to next step like designing framework then you must be familiar with excel file. We have to use this concept while designing Data Driven Framework as well.

Another point this is one of the most important questions in interviews as well and you should be having complete knowledge of it.

 

Read and Write excel files in Selenium

Step 1- Download apache poi jar file as below

Go to Official website of Apache POI and Click on the download section

http://poi.apache.org/download.html

Read and Write excel files in Selenium
Read/Write Excel file in Selenium

 

Now Click on the below mention link

Read and Write excel files in Selenium
Read/Write Excel file in Selenium

 

All jar files will come in zip files, Extract it and you will get final jar folder looks like this

Read/Write excel files in Selenium
Read/Write Excel file in Selenium

Add all jar files or below mention, jar files into Project.

 

Read/Write excel files in Selenium
Read/Write Excel file in Selenium

Step 2- How to add Jar files

Select project then Right click on project > Build path > Configure build path > Click on lib section > Add external jar
Precondition- Create a xlsx file and enter some data to read and save file at particular location.

 

Read Excel file using Apache POI

In below example, I am reading simple .xlsx file

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;


public class ReadandWriteExcel {

 public static void main(String []args){
  
  try {
  // Specify the path of file
  File src=new File("filepath/excelsheetname.xlsx");

   // load file
   FileInputStream fis=new FileInputStream(src);
 
   // Load workbook
   XSSFWorkbook wb=new XSSFWorkbook(fis);
   
   // Load sheet- Here we are loading first sheetonly
      XSSFSheet sh1= wb.getSheetAt(0);
 
  // getRow() specify which row we want to read.

  // and getCell() specify which column to read.
  // getStringCellValue() specify that we are reading String data.


 System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());

 System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());

 System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());

 System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());

 System.out.println(sh1.getRow(2).getCell(0).getStringCellValue());

 System.out.println(sh1.getRow(2).getCell(1).getStringCellValue());
 
  } catch (Exception e) {

   System.out.println(e.getMessage());

  }
  
 }
 
}

 

 

Write Excel file Selenium Webdriver

Now if you are familiar with reading excel then it is just a cup of tea now

In below example, I am  writing .xlsx file

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.testng.annotations.Test;

public class ReadandWriteExcel {

 public static void main(String []args){

  try {

  // Specify the file path which you want to create or write

  File src=new File("./testdata/test.xlsx");

  // Load the file

  FileInputStream fis=new FileInputStream(src);

   // load the workbook

   XSSFWorkbook wb=new XSSFWorkbook(fis);

  // get the sheet which you want to modify or create

   XSSFSheet sh1= wb.getSheetAt(0);

 // getRow specify which row we want to read and getCell which column

 System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());

 System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());

 System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());

 System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());

 System.out.println(sh1.getRow(2).getCell(0).getStringCellValue());

 System.out.println(sh1.getRow(2).getCell(1).getStringCellValue());

// here createCell will create column

// and setCellvalue will set the value

 sh1.getRow(0).createCell(2).setCellValue("2.41.0");

 sh1.getRow(1).createCell(2).setCellValue("2.5");

 sh1.getRow(2).createCell(2).setCellValue("2.39");


// here we need to specify where you want to save file

 FileOutputStream fout=new FileOutputStream(new File("location of file/filename.xlsx"));


// finally write content 

 wb.write(fout);

// close the file

 fout.close();

  } catch (Exception e) {

   System.out.println(e.getMessage());

  }

 }

}

 

Some companies also use CSV files to store the data and some also use Databases to store the data so based on your requirement you can select any data source.

I hope you have enjoyed the article if yes then share with your friends and colleagues as well.

Please comment in below section if you are facing any issue. Thanks For visiting my blog keep in touch

Filed Under: Advance Selenium Tagged With: Read External files

Reader Interactions

Comments

  1. RANJAN V says

    July 1, 2021 at 4:48 PM

    Nicely Explained))))

    Reply
  2. Akshata k says

    October 11, 2020 at 11:38 PM

    HI ,Please may i know how to write data to excel which is read
    from the webtable?

    Reply
    • Mukesh Otwani says

      October 11, 2020 at 11:57 PM

      Hi Akshata, what is the issue here?

      Reply
  3. Disha says

    October 3, 2020 at 12:09 PM

    Hi,

    getRow() and getCell() are non static methods that belongs to XSSFSheet class, as per java rule to invoke a non static method we need to create an object right but we are invoking getRow and get Cell() without creating object of XSSFSheet class.

    Can you please clear this doubt of mine?? Thank you in advance.

    Reply
    • Mukesh Otwani says

      October 5, 2020 at 9:59 AM

      Hi Disha,

      This is because of method chaining, we get the reference of class/interface and with that reference, we can call the methods.

      Reply
  4. Manmohan says

    March 20, 2020 at 7:22 PM

    Good evening sir
    help me for the below code
    using any loop to read all data from excel file instead of this much of line

    System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());

    System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());

    System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());

    System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());

    System.out.println(sh1.getRow(2).getCell(0).getStringCellValue());

    System.out.println(sh1.getRow(2).getCell(1).getStringCellValue());

    Reply
    • Mukesh Otwani says

      March 21, 2020 at 2:18 PM

      Hi Manmohan,

      Use 2 for loops. Outer for loop goes with row number and inner for loop does with column number

      Reply
  5. Leela says

    February 20, 2020 at 2:35 PM

    Hi MUkesh,

    Can you please share if you have article for reading the data from excel and passing it to my TestNG scripts. instead of hard coding the values like in case of username, password etc.

    Thanks in advance
    Leela

    Reply
    • Mukesh Otwani says

      February 20, 2020 at 7:54 PM

      Hi Leela,

      Apologies, I don’t have such post as it is custom requirement which is part of framework design. The framework could be of many ways because it depends business requirements. You can create some utility methods which can read required data from excel file and feed them into test class/methods.

      Reply
      • Ketam Srinivas says

        March 4, 2020 at 7:52 AM

        This is the Utility method, Reading data from excel dynamically hope this helps you.
        public String getRowAndCoulmn(int row, int column) {
        File src = new File(filePath);
        FileInputStream fis = new FileInputStream(src);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet = wb.getSheetAt(0);
        String getRowAndColumn = sheet.getRow(row).getCell(column).getStringCellValue();
        fis.close();
        return getRowAndColumn;

        }

        Reply
        • Mukesh Otwani says

          March 4, 2020 at 10:45 AM

          Hi,

          Before calling String getRowAndColumn = sheet.getRow(row).getCell(column).getStringCellValue(); statement, check for Cell type

          Reply
          • Ketam srinvias says

            March 5, 2020 at 8:13 AM

            Thank you for the Suggestion. I do go with that way, Once. But what is the main use of, checking the celltype.

          • Mukesh Otwani says

            March 5, 2020 at 4:00 PM

            Hi Ketam,

            Based on Cell type only, you can call which method to read what type of data from the corresponding cell. Because excel cell type could of type date, numeral, general…etc

          • Mukesh Otwani says

            March 5, 2020 at 4:00 PM

            Hi Ketam,

            Based on Celltype only, you can call which method to read what type data from corresponding cell. Because excel cell type could of type date, numeral, general…etc

  6. Sai says

    February 10, 2020 at 2:36 AM

    Hi Mukesh,

    I have test case in one test package, login page in another package, test utilities in another package.

    What should I write under @dataProvider in my test case ? I am calling my login page method from my test case, and I am calling data provider method from login page.

    In this case, what should I mention (@dataProvider = “?” ) in test case and @dataProvider (name = “?”) in my login page method?

    I have 2 classes in utilities package.
    1. ExcelUtils – Contains getRowCount, getColCount, getCellData methods
    2. ExcelDataProvider – testData method to call ExcelUtils methods

    Sorry, I am confused in what to write and where . I am using hybrid framework.

    Please help

    Reply
    • Mukesh Otwani says

      February 10, 2020 at 1:59 PM

      Hi Sai,

      Check this link from official TestNG Documentation https://testng.org/doc/documentation-main.html#parameters-dataproviders

      Reply
  7. XBlade says

    December 5, 2019 at 4:44 PM

    HEY MUKESH

    CAN YOU MAKE A VIDEO OR ARTICLE ON, HOW TO READ AND WRITE THE EXCEL FILE
    OF EVERY DATA TYPE IN EXCEL SHEET FOR EXAMPLE(DATE, INT, STRING, DOUBLE ETC).
    YOU ARE JUST MAKING A READ EXCEL OF STRING TYPE, BUT I WANT TO KNOW HOW U CAN READ EVERY TYPE OF DATA FROM EXCEL…

    PLEASE REVERT
    NEED HELP

    Reply
    • Mukesh Otwani says

      December 5, 2019 at 9:03 PM

      Hi Paras,

      I’ll post it soon…:)

      Reply
  8. swetha says

    November 5, 2019 at 4:35 PM

    HI Mukesh,

    I just want to know how to read row data and column data saparetley in excel file using selenium automation

    Reply
    • Mukesh Otwani says

      November 5, 2019 at 5:36 PM

      Hi Swetha,

      The way you traverse in Matrix using row following by column the same way you need to mention till which column need to address.

      Reply
  9. Chaitanya says

    September 25, 2019 at 4:48 PM

    Hi Mukesh,

    Can you please suggest me how to read data from excel sheet and write it in xml file.

    Reply
    • Mukesh Otwani says

      September 25, 2019 at 5:00 PM

      Hi Chaitanya,

      Since you already visited my blogpost, for xml part you can refer this link https://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/

      Reply
  10. Kiruthika says

    August 29, 2019 at 4:05 PM

    Hi,
    Can you plz suggest me how to read data from ods file.I tried installing Apache poi ods toolkit but didn’t workout.

    Reply
    • Mukesh Otwani says

      August 29, 2019 at 5:29 PM

      Hi Kiruthika,

      Apache POI Toolkits are already outdated for Open Office/Libre Office file formats. You can try with jOpenDocument library but I am not sure fully about it.

      Reply
  11. Bittu says

    July 30, 2019 at 3:40 AM

    Hi Mukesh, i am still getting error while writing in excel sheet. The file gets corrupted

    Error
    DOMSource cannot be processed: check that saxon8-dom.jar is on the classpath
    Fail to save: an error occurs while saving the package : The part /docProps/core.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller@657c8ad9

    Reply
    • Mukesh Otwani says

      July 30, 2019 at 7:40 PM

      Hi Bittu,

      I hope that you are using Apache POI 3.17 version with Java 8.

      Reply
      • Bittu says

        July 30, 2019 at 8:09 PM

        Hi Mukesh,

        I used apache poi 4.0 and 3.4 both, added in build path ..external jars

        Thanks.

        Reply
        • Mukesh Otwani says

          July 30, 2019 at 9:25 PM

          Hi Bittu,

          Use only 3.7 only.

          Reply
  12. Niketan says

    July 24, 2019 at 4:49 PM

    Hi Mukesh,
    Can you tell me how to write a long data in excel sheet.

    Reply
    • Mukesh Otwani says

      July 26, 2019 at 12:01 AM

      Hi Niketan,

      Excel itself will except max of 15 digits numerical value which it can keep without changing to another format. Open excel file -> right click on any cell -> Format Cells -> select Number with Decimal places as ‘0’ -> Ok

      Reply
  13. suresh says

    July 24, 2019 at 2:11 PM

    hi,

    Through excel file username is fetching as integer…for ex: if username is 123456789 in excel sheet but while running script it coming as “Cannot get a STRING value from a NUMERIC cell”

    driver.findElement(By.xpath(“//input[@id=’ctl00_ContentPlaceHolder1_UserName’]”)).sendKeys(cell.getStringCellValue());

    Please give solution for this

    Reply
    • Mukesh Otwani says

      July 24, 2019 at 2:48 PM

      Hi Sureshh,

      You have to use cell.getNumeriellValue because Apache POI reads always data as per cell type. Once you get double value, convert it to String type like
      String s = String.valueOf(double) and proceed with sendKeys

      Reply
      • suresh says

        July 25, 2019 at 7:21 PM

        Hi Mukesh,

        in excel integer value : 917232345267

        WebElement str=driver.findElement(By.xpath(“//input[@id=’ctl00_ContentPlaceHolder1_UserName’]”));
        str.sendKeys(cell.getNumericCellValue()+”(Integer)”);

        after executing getting value as 9.17232345267E1

        pls suggest

        Reply
        • Mukesh Otwani says

          July 25, 2019 at 11:17 PM

          Hi Suresh,

          Use this new java.text.DecimalFormat(“0”).format(cell.getNumericCellValue()) where cell is of type Cell

          Reply
  14. deepak gupta says

    July 23, 2019 at 8:02 PM

    hi i m having excel sheet with three rows,
    1st and 2nd with incorrect user name and password
    3rd with correct username n password
    want to use this excel data into web application trying all the data with result pass and failed updated excl

    Reply
    • Mukesh Otwani says

      July 23, 2019 at 10:02 PM

      Hi Deepak,

      This link will help you.

      Reply
  15. Suraj says

    June 4, 2019 at 11:52 AM

    Hi mukesh..

    Why you use try catch in every program

    Reply
    • Mukesh Otwani says

      June 4, 2019 at 1:58 PM

      Hi Suraj,

      Try Catch block is used to handle exception as situation may come in future if not present, where Exception might be thrown which you need to handle to avoid unnecessary termination of code.

      Reply
  16. Nehal says

    June 2, 2019 at 2:03 AM

    Through excel file username is fetching as decimal…for ex: if username is 1234 in excel sheet but while running script it coming as 1234.0
    Please give solution for this

    Reply
    • Mukesh Otwani says

      June 3, 2019 at 10:44 AM

      Hi Nehal,

      Apache POI reads numerical data as double so in your case convert it into int like
      double data = 1234.0
      int value = (int)data this will make it to 1234 so you will get rid of decimal part
      later on convert this into String(if it is required)

      Reply
  17. sree says

    May 19, 2019 at 12:08 PM

    Hi Mukesh,

    I need to search for a particular string in entire excel sheet and get the value from other column from the same row. I have 5-6 rows with that particular string. And compare the data in another sheet which is also having large data and matched with multiple rows. How an I do this? Can you please help?

    Reply
    • Mukesh Otwani says

      May 19, 2019 at 9:50 PM

      Hi Sree,

      If you have big amount of data into excel then it is nor recommendable to use excel because it slows whole process. But still you proceed with excel file and you can observe how much considerable time it takes to complete your verification step. Mean while you can also try Fillo library. Because in future if your data moves into database then this library will help you alot.

      Reply
  18. sukhvir says

    May 15, 2019 at 12:36 AM

    hi
    i am trying to write a excel file. but it does not write if the file is empty means dont have any data. it returns null.if it has data then it writes. whats the reason.

    Reply
    • Mukesh Otwani says

      May 15, 2019 at 6:19 PM

      Hi Sukhvir,

      What exception message are you getting ?

      Reply
    • vikas says

      May 25, 2019 at 2:44 AM

      Hi Sukhvir,

      You need to write if condition for that, means if your row or column is empty then perform your respective actions.

      Reply
  19. Naveen M says

    April 20, 2019 at 5:48 PM

    Hi Mukesh,

    I’m trying to read the data using jexcel by following one of your video in youtube. But i’m getting the following error in the console. ‘

    Command format: Demo [-unicode] [-csv] [-hide] excelfile
    Demo -xml [-format] excelfile
    Demo -readwrite|-rw excelfile output
    Demo -biffdump | -bd | -wa | -write | -formulas | -features | -escher | -escherdg excelfile
    Demo -ps excelfile [property] [output]
    Demo -version | -logtest | -h | -help

    Please help me in solving the issue

    Reply
    • Mukesh Otwani says

      April 23, 2019 at 12:02 AM

      Hi Naveen, Please use Apache POI for reading excel now.

      Reply
  20. Rajnish says

    April 9, 2019 at 11:57 AM

    Hi Mukesh, please help me to import the links on excel sheet. actually, I am working on the program where I get all links on my console of selenium but I want to write these links import on the excel sheet.

    Reply
    • Mukesh Otwani says

      April 9, 2019 at 1:08 PM

      Hi Rajnish,

      As you already mentioned that you are getting all links on console so you can redirect same to write into excel file. Only thing which you need to ensure is, keep on incrementing row number after each iteration.

      Reply
  21. Ghouse says

    April 1, 2019 at 11:26 AM

    Hi Mukesh,

    Is there a way of renaming the same Excel file after editing?

    Reply
    • Mukesh Otwani says

      April 1, 2019 at 4:48 PM

      Hi Ghouse,

      Java provides built in method for renaming file from file explorer. Please check this link https://www.journaldev.com/836/java-rename-file-jave-move-file

      Reply
    • Mukesh Otwani says

      April 1, 2019 at 4:50 PM

      Hi Ghouse,

      Java provides built in method for renaming file from file explorer

      Reply
  22. Navneet says

    March 30, 2019 at 11:47 PM

    Hi I am trying to load properties file in selenium like below:
    prop = new Properties();
    File src=new File(“C:\\Users\\Dell\\eclipse-workspace\\ProjectTestPack\\src\\main\\java\\com\\auto\\qa\\config”);
    FileInputStream ip = new FileInputStream(src);
    prop.load(ip);
    Getting below error please help on this
    java.io.FileNotFoundException: C:\Users\Dell\eclipse-workspace\ProjectTestPack\src\main\java\com\auto\qa\config (Access is denied

    Reply
    • Mukesh Otwani says

      March 31, 2019 at 9:16 AM

      Hi Navneet,

      Please provide full path of config.properties file like C:\\Users\\Dell\\eclipse-workspace\\ProjectTestPack\\src\\main\\java\\com\\auto\\qa\\config.properties

      Reply
  23. Nithya says

    February 24, 2017 at 10:39 AM

    Appreciate your effort.Clear explanation.Easy to follow.Thanks

    Reply
    • Mukesh Otwani says

      February 24, 2017 at 11:07 AM

      Thanks Nithya..:)

      Reply
  24. Nikhil says

    February 12, 2017 at 12:06 AM

    Hello Mukesh,

    You have shared pretty much important stuff.
    I have question while reading data from excel sheet. How to handle blank cells for xSSF workbook. I am searching a alot for this. But cudnt get anything helpful.

    Thanks in advance.

    Reply
    • Mukesh Otwani says

      February 12, 2017 at 3:46 PM

      Hi Nikhil,

      Please check this link http://poi.apache.org/spreadsheet/quick-guide.html#Iterator

      Reply
  25. Dinesh Guptha Sivaguru says

    January 25, 2017 at 8:48 PM

    Hi Mukesh,

    Do you have any materials or videos available to write the code for to fetch the web table data?

    Reply
    • Mukesh Otwani says

      January 26, 2017 at 6:09 PM

      Hi Dinesh,

      Web Table html design differs in every web applications. First of all you need to find out the total number of rows available in web table. Then based on tr iterate through td. Finally do getText() or getAttribute(“innerHTML”) to get actual content from web table.

      Reply
      • Dinesh Guptha Sivaguru says

        January 26, 2017 at 7:03 PM

        Ok, Mukesh. I will try and let you know and thanks for your immediate responses.

        Reply
        • Mukesh Otwani says

          January 26, 2017 at 10:12 PM

          Sure…!!!

          Reply
  26. Narayan Boolchandani says

    January 25, 2017 at 6:12 PM

    Hi Mukesh
    Very useful and informative content you share in Learn-automation portal. One thing I am facing problem that i want to fetch the xpath and text values from UI and write it to in excel sheet. Please suggest how should i do this, and if you may provide framework than it will help me.
    Thanks a lot.

    Reply
    • Mukesh Otwani says

      January 28, 2017 at 6:10 PM

      Hi Narayan,

      Xpath for you can find using firepath plugin in firefox and to get text values in those fields, you can make use of getAttribute() where you can provide property name against which required value exists.

      Reply
  27. Waheed Ahmed says

    January 23, 2017 at 10:19 AM

    The User name and password for login is stored in excel sheet in two columns. I want to read the username/password from the excel sheet. If it’s a valid user name/pwd I just wanted to Write “Pass” in new column else I need to write it as “Fail” . Write pass and fail based on succeful login into excel file not hardcoding values. How to do it?

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:52 PM

      Hi Ahmed,

      This is what you are looking http://learn-automation.com/data-driven-framework-in-selenium-webdriver/

      Reply
      • Waheed Ahmed says

        January 24, 2017 at 5:06 PM

        Thank you Mukesh for your kind pointer.. I have watched that video .. What I am looking for is once first UserName and Password combination is validated successfully in excel file in the corresponding column it should show PASS else show Failed…

        Reply
        • Mukesh Otwani says

          January 24, 2017 at 5:12 PM

          Yes for that you need to write logic 😉

          Reply
  28. Dinesh Guptha Sivaguru says

    January 22, 2017 at 11:40 PM

    Hi Mukesh,

    First, I would like to say thanks for doing this great video presentation and these videos will be very useful for beginners like me.

    My Q is: I want to write the web table data into an excel sheet as it is on the web page.Could you please let me know how to proceed on this?

    Reply
    • Mukesh Otwani says

      January 23, 2017 at 8:29 PM

      Hi Dinesh,

      For reading data from WebTable, you need to use two loops(one nested inside another) in which outer iterates row and inner one for column. You can use same loop for iterating through excel row followed by column number.

      Reply
  29. Daisy G says

    January 21, 2017 at 6:40 PM

    Hi Mukesh, nice tutorial. Is there a way to directly read a column value from excel without iterating through all the columns or hard coding the column index in getcell().

    Reply
    • Mukesh Otwani says

      January 21, 2017 at 7:28 PM

      Hi Daisy,

      Reading a cell value from an excel file is just accessing a cell from a two dimensional matrix. In 2D matrix, you can iterate based on row number followed by column number. Similarly, if you want to access any cell then obviously row number followed by column number is must. One more thing, without creating workbook object, it is not possible to access any cell inside excel file and this is what Apache POI defined.

      Reply
  30. khushali says

    January 17, 2017 at 12:57 PM

    i m fetching the value from the excel but it comes into the float.and i want value in int. so i used getnumericalvalue(). but its gives me an error. tell me how to convert it.

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 5:02 PM

      Try this http://learn-automation.com/read-numeric-data-excel-using-apache-poi-selenium-webdriver/

      Reply
  31. Lavanya says

    January 16, 2017 at 5:55 PM

    Hi Mukesh,
    with this blog and the videos you have created, i have learned much about selenium .

    I need a help for the below code:
    if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
    String str = NumberToTextConverter.toText(cell.getNumericCellValue())
    }
    Error is:
    1.cell.getCellType() : it shows as deprecated.
    how to fix this?
    2.Cell.CELL_TYPE_NUMERIC)
    Not found Exception displayed
    please help me to fix these two!!

    Regards
    Lavanya

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 5:01 PM

      Hi Lavanya,

      this will help http://learn-automation.com/read-numeric-data-excel-using-apache-poi-selenium-webdriver/

      Reply
  32. Anjana says

    December 15, 2016 at 10:47 AM

    Hi Mukesh
    My code for reading and writing on excel works fine with try and catch exception included but my code shows error without it.
    Can you help me to figure out the reason behind it.

    Reply
    • Mukesh Otwani says

      December 17, 2016 at 12:43 PM

      Hi Anjana,

      It is because it will throw checked exception so if we dont handle then we can’t run our scripts.

      Reply
  33. bhavani says

    November 30, 2016 at 1:23 PM

    Hi,This site is very useful,

    i need to read multiple data from excel sheet(apache poi) ,m unable to write.ex:
    login application->open some link->fill employee form->close->again login with other employee->open link->fill form->aftre filling form ->again fill other details

    Reply
    • Mukesh Otwani says

      November 30, 2016 at 4:57 PM

      Hi Bhavani,

      You have to follow below link for above task

      http://learn-automation.com/data-driven-framework-in-selenium-webdriver/

      Reply
  34. harshal says

    November 28, 2016 at 11:45 AM

    hi mukesh,
    1) is this possible to automate any type of captcha??
    2) Is this possible to Read and write excel simentenously??

    Reply
    • Mukesh Otwani says

      November 30, 2016 at 7:32 PM

      Hi Harshal,

      1- No
      2- No, both can be done individually.

      Reply
  35. arjun says

    November 21, 2016 at 9:45 PM

    Very Nice tutorial Mukesh sir

    Reply
  36. Rithesh says

    November 7, 2016 at 9:14 AM

    I used CreateRow instead of getRow, now its working fine.

    Reply
    • Mukesh Otwani says

      November 30, 2016 at 7:57 PM

      Cool

      Reply
  37. Rose says

    October 28, 2016 at 11:36 AM

    Hi Mukesh-
    I already figured out the solution. Thanks.

    Reply
    • Mukesh Otwani says

      November 4, 2016 at 10:41 PM

      cheers

      Reply
  38. Bhushan Patil says

    October 19, 2016 at 6:04 PM

    Hi Mukes h
    How can automate captcha??

    Reply
    • Mukesh Otwani says

      October 20, 2016 at 1:58 PM

      No Bhushan

      Reply
  39. Jithin says

    October 17, 2016 at 7:05 PM

    How to refresh an Excel file Using Apache POI

    Reply
    • Mukesh Otwani says

      October 18, 2016 at 2:07 PM

      It refresh excel automatically

      Reply
  40. akash gupta says

    October 10, 2016 at 7:07 PM

    hi mukesh,
    actually, I am a manual tester having 2 years of experience. Now, I am learning selenium from your videos and literally saying, these are fantastic.

    But I have a question:
    I am not good in fetching data from excel through apache poi. can u please give mail a complete video of how to read multiple sets of data for a test case from excel sheet.

    Reply
    • Mukesh Otwani says

      October 12, 2016 at 11:44 AM

      Hi Akash,

      You can follow below link for advance data fetching http://learn-automation.com/data-driven-framework-in-selenium-webdriver/

      Reply
  41. satish says

    October 6, 2016 at 5:27 PM

    When i am reading data from excel. I am getting this error message. for loop was not running second time.
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 2
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:318)
    at testpackage.excelwebrerad.main(excelwebrerad.java:40)

    Reply
    • Mukesh Otwani says

      October 7, 2016 at 7:30 PM

      Hi Satish,

      It seems you have used packages of JXL jars too. Make sure only POI jars added and then run the script.

      Reply
  42. Bjoern says

    September 29, 2016 at 9:58 AM

    Dear Mukesh,

    i am looking for a way to get the results from a test in selenium ide out to an excel sheet. i found your video instructions on Jxcl and Apache POI but am missing the link between the excel issue and the results i got from the echo command in the selenium ide shell.

    This is the data i am looking for to export:

    [info] Playing test case
    [info] Executing: |echo | ${Nettopreis} | |
    [info] echo: € 817,68*
    [info] Executing: |echo | ${EAN} | |
    [info] echo: 4035694005136
    [info] Executing: |echo | ${Versand} | |
    [info] echo: Frei Haus
    [info] Executing: |echo | ${Lieferzeit} | |
    [info] echo: 38 Tage

    what is the recommended procedure? thank you for help
    regards
    Björn

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 10:32 AM

      Hi Bjoern,

      I am not good in Selenium IDE so not sure on this 🙁 Just a quick question why you are still trying with Selenium IDE? You can switch back to webdriver and you can play with all features.

      Reply
      • Bjoern says

        September 29, 2016 at 10:56 AM

        Hi Mukesh,
        thanks for answering quickly. I liked the easy way to use the selenium ide, it is very visual and you can see the steps/performance at once.
        you mean using webdriver in java? with your videos i now know to read and write excel sheets in java, but i am not clear about how the connection to selenium works. do you have a hint, pls?

        Reply
        • Mukesh Otwani says

          September 29, 2016 at 11:02 AM

          Yes sure I have shared some videos with you. Kindly check ur email 🙂 If you did not get any email then let me know.

          Reply
          • Bjoern says

            September 29, 2016 at 11:08 AM

            thank you Mukesh, received.

          • Mukesh Otwani says

            September 29, 2016 at 12:44 PM

            Cheers

          • Bjoern says

            September 29, 2016 at 12:33 PM

            Got an error message: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms

            I am using Eclipse Neon Release (4.6.0).

          • Mukesh Otwani says

            September 29, 2016 at 12:42 PM

            Hi Bjoern,

            Here is the solution http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/

          • Bjoern says

            September 29, 2016 at 1:01 PM

            Thanks again, unfirtunately i still have the same error:
            org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms

            it further states:

            Firefox console output:
            — see http://www.mozilla.com/en-US/blocklist/
            LdrLoadDll: Blocking load of ‘pmls.dll’ — see http://www.mozilla.com/en-US/blocklist/

            thank you for assisting.

          • Mukesh Otwani says

            September 29, 2016 at 2:08 PM

            It seems FIrefox port is blocking. Can you try the same in Chrome and IE

            http://learn-automation.com/launch-chrome-browser-using-selenium-webdriver/

          • Bjoern says

            September 29, 2016 at 2:18 PM

            That was the solution! Thank you! ist this a temporary issue of FF? what do you think?

          • Mukesh Otwani says

            September 29, 2016 at 2:21 PM

            Yes I think so. I am using 46 version of FF and its working smoothly.

          • Bjoern says

            September 29, 2016 at 3:13 PM

            Yes, i have read that in other forums, too. Consider to downgrade, is that recommendable?

            other issue: i was thinking about exporting my test from Selenium IDE to Eclipse and then append the code with functions to print data to an excel sheet. might that work?

            Selenium IDE offers different export options to java. which one should i use?

            Java/JUnit4/WebDriver

            Java/JUnit4/WebDriver Backend

            Java/JUnit4/Remote Control

            Java/JUnit3/Remote Control

            Java/TestNG/WebDriver

            Java/TestNG/Remote Control

            Thanks for helping.

          • Mukesh Otwani says

            September 29, 2016 at 8:34 PM

            Select Java/TestNG/WebDriver only for converting.

          • Bjoern says

            September 29, 2016 at 3:36 PM

            Hello, again,
            i chose the first option called “JUnit4/WebDriver”, imported and opened it with Eclipse and run. Produced the result:
            “No test found with test runner JUnit4”

            am i missing the point here? how do i get the results?
            k.r. Björn

          • Mukesh Otwani says

            September 29, 2016 at 8:36 PM

            It seems you have used JUNIT, Kindly select TestNG and then run the test.

            Note- Make sure you have installed TestNG in your eclipse if not then use below link to install http://learn-automation.com/how-to-install-testng-in-eclipse/

        • Gaurav Khurana says

          December 7, 2016 at 2:54 PM

          what a nice discussion between Mukesh and his reader,,,, Hats off !!!

          Reply
          • Mukesh Otwani says

            December 14, 2016 at 12:13 AM

            Thanks Gaurav

  43. ajit says

    September 20, 2016 at 3:58 PM

    Hi Mukesh ,
    I am trying to to fetch the single web Element and store it into Excel file
    so far i got success up to print the output on the console but i need to write into the Excel file.
    any suggestions will be great help

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:30 PM

      Yes Ajit it quite easy. What issues you are getting?

      Reply
  44. Rahul says

    September 6, 2016 at 11:40 AM

    How to fetch multiple data types from excel .For ex. In your blog it is mentioned as “getRow(0).getCell(0).getStringCellValue()); ” .If in the excel there are string,Int ,DOuble etc how can we fetch it in for loop ?

    Reply
    • Mukesh Otwani says

      September 7, 2016 at 11:53 PM

      Hi Rahul,

      to get numeric value use getNumericCellvalue method

      Reply
  45. Chino says

    September 2, 2016 at 3:42 PM

    Hi Mukesh. It’s my first time in reading and writing excel file in Selenium. So for practice purposes, I just copied your example code. I have already setup and installed the POI jars. I wonder why the code below returned null. Thanks

    package files;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class WriteExcel {

    public static void main(String[] args) {

    try{
    File excel = new File(// excel path in (.xlsx) file format));
    FileInputStream fis = new FileInputStream(excel);
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sh = wb.getSheetAt(0);

    sh.getRow(1).createCell(0).setCellValue(“user_101”);

    FileOutputStream fos = new FileOutputStream(excel);
    wb.write(fos);
    wb.close();

    }
    catch(Exception e){
    System.out.println(e.getMessage());
    }

    }

    }

    Reply
    • Mukesh Otwani says

      September 2, 2016 at 5:15 PM

      Hi Chino,

      Have you changed the excel file path in below link File excel = new File(// excel path in (.xlsx) file format));

      Reply
      • Chino says

        September 5, 2016 at 3:25 PM

        Take a look at this one, surrounded it with TRY..CATCH block.

        a. The e.getMessage returns null
        b. Should we close the workbook, or the fileoutputstream?

        package files;

        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileOutputStream;

        import org.apache.poi.xssf.usermodel.XSSFSheet;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;

        public class WriteExcel {

        public static void main(String[] args) {

        try{
        File excel = new File(“D:\\NINZO\\RC_automation_tests\\GigMeOnline_PageFactory\\Gigme_UserCredentials.xlsx”);
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sh = wb.getSheetAt(0);

        sh.getRow(1).createCell(0).setCellValue(“user_101”);

        FileOutputStream fos = new FileOutputStream(excel);
        wb.write(fos);
        fos.close();

        }
        catch(Exception e){
        System.out.println(e.getMessage());
        }

        }

        }

        Reply
        • Mukesh Otwani says

          September 7, 2016 at 11:55 PM

          Hi,

          make below changes

          old code fos.close();
          new code wb.close();

          Reply
  46. Anoop says

    August 29, 2016 at 4:41 PM

    Hello ,

    Can we Read/Write data from open office document having extension like “.ods”.
    Please advise how to do that.

    Thanks for good tutorial.

    Reply
    • Mukesh Otwani says

      September 8, 2016 at 1:21 AM

      yes anoop we can do that. You can use HSSFWOrkbook class for this.

      Reply
  47. sudhr says

    August 23, 2016 at 4:43 PM

    11.Suppose you have 3000 test data, can you automate using Excel sheet.How ? are there any drawbacks

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:19 PM

      I used excel and I don’t find any drawbacks. You can create some reusable methods which will accept Sheet name and row,column as parameter and will return the date,.

      Reply
  48. Jayesh says

    July 28, 2016 at 7:08 PM

    Hi Mukesh, How can we insert a column at the start in the existing excel sheet?

    Reply
    • Mukesh Otwani says

      August 2, 2016 at 12:35 PM

      Hi Jayesh,

      You can use createCell to create column in excel

      Reply
  49. sudhr says

    July 22, 2016 at 5:22 PM

    how to get all the values from excel sheets and all types(string,int…) of values

    Reply
    • Mukesh Otwani says

      July 25, 2016 at 12:13 PM

      you can create custom method which will do this. COde will be something like this.

      if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
      String str = NumberToTextConverter.toText(cell.getNumericCellValue())
      }

      Reply
  50. shil says

    July 19, 2016 at 11:20 AM

    Hi Mukesh,
    I have a query.
    I need to fetch the data from an excel as dump. but i wanted to ignore/skip one specific column. How to do this?

    Reply
    • Mukesh Otwani says

      July 20, 2016 at 1:54 PM

      Hi Shilpa,

      I did not understand by work dump 🙁 What is the exact requirement.

      Reply
  51. zainkk says

    April 26, 2016 at 7:46 AM

    Dear Mukesh,
    i was trying to write comments on “For better Reporting Capture Screenshot Selenium Webdriver” URL “http://learn-automation.com/how-to-capture-screenshot-in-selenium-webdriver/”
    i am facing the problem on FileUtils class is not showing me and ever its methods, pic 1 and if i am trying to selecting FileUtils (org.apachecommons.io) its writing script pic 2 “org.apache.commons.io.FileUtils” then showing all methods but not taking screen shots.
    i drooped an email to you also, thanks in advance.

    Reply
  52. Satyam Kumar says

    April 1, 2016 at 3:06 PM

    Hi Mukesh,

    I tried to read data from Excel sheet, which is working fine.

    While I tried to get Numeric data like phone number and use it in sendKeys, I am getting “Exception in thread “main” java.lang.IllegalStateException: Cannot get a text value from a numeric cell” error.

    Please help me in resolving the issue.

    Thanks.

    Reply
    • Mukesh Otwani says

      April 1, 2016 at 5:00 PM

      Hi Satyam,

      Use getNumericCellvalue to read data in case digit.

      Sample code

      double data=rest-of-the-code.getNumericCellvalue();

      String phoneNumber=Double.toString(data);

      Reply
  53. harsh says

    March 23, 2016 at 2:56 PM

    hi mukesh want to know how to read from excel and the data in excel is a user input and perform operation and print the output in that same spreadsheet

    Reply
    • Mukesh Otwani says

      March 26, 2016 at 2:00 PM

      Hi Harsh,

      Have u tried the code from u your side? What issue you are facing ?

      Reply
  54. sunita says

    February 12, 2016 at 11:20 AM

    hii mukesh…thanks for sharing such a nice tutorial…
    i m getting error while writing in excel file its saying Can’t open the specified file: ‘C:\sunita_java\ta.xlsx
    and when i try to open my excel file its giving file is corrupt or file format is incorrect.

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:15 PM

      Hi Sunita,

      sorry for late reply. is this fixed?

      Reply
      • dummy1333 says

        March 4, 2016 at 2:26 AM

        hi, i want to know How can i parameter login info & screenshot taken in POM

        Reply
        • Mukesh Otwani says

          March 7, 2016 at 11:00 AM

          Hi,

          Below link will help you

          Reply
      • Geetha says

        March 19, 2016 at 12:10 AM

        Hi Mukesh, I created excel sheet using Selenium webdriver script, but when I try to attach it, it is saying that the format is incorrect. My logic is to create a xlsx using code, do the read/write operation and later delete it. So, each time, I want to create a new xlsx file. Do you have any soultion for this ?

        Reply
        • Mukesh Otwani says

          March 20, 2016 at 7:22 PM

          Hi Geetha,

          Check the version and check manually are you able to open the file

          Reply
  55. vijay cyrus says

    February 4, 2016 at 11:24 AM

    Nice content Mukesh, I can read/write excel sheets using these tutorials. Great work. Keep it up..

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:16 PM

      Hi Vijay,

      Cheers. Please explore other topics as well.

      Reply
  56. Deepak says

    January 4, 2016 at 7:24 PM

    Actually i’m getting an error “Error: Could not find or load main class ReadExccel.ReadExcelData”, although i have written the code same as your’s…there is no compilation issue.

    Reply
    • Mukesh Otwani says

      January 6, 2016 at 1:01 AM

      Hi Deepak,

      Please visit https://www.youtube.com/watch?v=sbBdj4zIMqY

      Reply
  57. Enamul Hoque says

    January 4, 2016 at 1:42 AM

    I like your video lectures. These are explained in such a way that the tutorials become very easy to understand.

    Reply
    • Mukesh Otwani says

      January 6, 2016 at 1:04 AM

      Thanks Enamul, Glad to know that you Liked it 🙂 keep visiting.

      Reply
      • Malaisamy J says

        May 17, 2016 at 2:53 PM

        Hi Mukesh,

        I have few doubt. Can we open excel file and write whatever we want?

        Through Selenium webdriver.

        Thanks,
        Malaisamy J

        Reply
        • Mukesh Otwani says

          May 19, 2016 at 4:28 AM

          Hi Malaisamy J,

          You can do in diff sheet.

          Reply
  58. Ranu says

    January 3, 2016 at 11:22 AM

    Hi Mukesh,

    As i seen you created a library for read excel and than explained how to use it in any program but same i am not getting for write excel. Can you please provide any reference.

    Reply
    • Mukesh Otwani says

      January 4, 2016 at 1:27 AM

      Hi Ranu,

      Have you tried the code from your end if yes then please send me your code I will correct it and will revert back.

      Reply
  59. sai says

    December 16, 2015 at 8:09 AM

    hii can u please give me code to validate the login functionality with different usernames and passwords from xlsx file
    and it should store the result in the same file

    Reply
    • Mukesh Otwani says

      December 17, 2015 at 8:47 PM

      Hi Sai,

      THis is called DDF and I have covered the same in https://www.youtube.com/watch?v=ORscNOhgW4w

      Writing result in the same file will upload by this weekend.

      Reply
  60. mashres says

    December 2, 2015 at 1:17 AM

    Hello Mukesh

    when i run the result is not passed to the excel sheet

    Reply
    • Mukesh Otwani says

      December 2, 2015 at 1:27 AM

      Hi Mahesh,

      Sorry I did not get your doubt. Can you explain in detail?

      Reply
  61. Raghavendra says

    October 14, 2015 at 4:15 PM

    Script to copy all sheets from a excel to single sheet in new excel file
    package ExcelAutomation;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import org.testng.annotations.Test;
    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;

    import testBase.PhyWebTestBase;
    import utility.PhyWebUtil;

    public class CopyExcel extends PhyWebTestBase
    {
    public CopyExcel() throws Exception
    {
    super();
    }

    @Test
    //String sheetName1=”1 New and Changed Brands,Form or Strength and Manufacturers”;
    public void CopyFile() throws Exception
    {
    try
    {
    String sheetName1=”1 New and Changed Brands,Form or Strength and Manufacturers”;
    String srcFilePath = “/Users/raghavendra/Automation/Scripts/4 Weekly Database Changes/Old Databases/885/2015_09_29_Changed_Data_For_Release_885.xlsx”;
    String destFilePath = “/Users/raghavendra/Automation/Scripts/4 Weekly Database Changes/Old Databases/885/1 New and Changed Brands,Form or Strength and Manufacturers1.xls”;
    String sheetName,cellData = null;
    int a,row = 0,row1=0,colCount = 0,newRowCount;
    double cellData1 = 0;
    XSSFSheet sheet1 = null;
    XSSFWorkbook workbook1 = null;
    XSSFRow rowData1 = null;
    FileInputStream inputStream = new FileInputStream(new File(srcFilePath));
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
    FileOutputStream destFile;
    destFile=new FileOutputStream(new File(destFilePath));
    workbook1 = new XSSFWorkbook();
    for(sheetnum=0;sheetnum<=6;sheetnum++)
    {
    XSSFSheet sheet=workbook.getSheetAt(sheetnum);
    sheetName=sheet.getSheetName();
    if(sheetName.equals("New Brands") || sheetName.equals("Changed Brands") || sheetName.equals("New Form Strength") || sheetName.equals("Changed Form Strength") || sheetName.equals("New Manufacturers") || sheetName.equals("Changed Manufacturers"))
    {
    sheet=PhyWebUtil.getSheetFromExcel(srcFilePath, sheetnum);
    rowCount=sheet.getLastRowNum();
    rowCount=rowCount+1;
    newRowCount = row+rowCount;
    if(sheetName.equals("New Brands"))
    sheet1 = workbook1.createSheet(sheetName1);
    if(sheetName.equals("New Brands")|| sheetName.equals("Changed Brands") || sheetName.equals("New Form Strength") || sheetName.equals("Changed Form Strength"))
    colCount=5;
    else if(sheetName.equals("New Manufacturers") || sheetName.equals("Changed Manufacturers"))
    colCount=3;
    for(row = row1;row<=newRowCount-1;row++)
    {
    rowData1 = sheet1.createRow(row1);
    row=0;
    for(col=0;col1 && row==row1)
    rowData1.createCell(row+1).setCellValue(sheetName);
    else
    rowData1.createCell(col+1).setCellValue(cellData);
    }
    if(row==newRowCount)
    a=1;
    }
    }
    }
    row1=row+1;
    }//End for1
    inputStream.close();
    workbook1.write(destFile);
    }//try end
    catch (Exception e)
    {
    System.out.println(e.getMessage());
    }
    }
    }

    Reply
    • Mukesh Otwani says

      October 14, 2015 at 11:32 PM

      What is the issue in this?

      Reply
  62. Dhruti says

    September 23, 2015 at 11:24 PM

    I am trying to to cell and getting error. can I g help here please.

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FilterInputStream;
    import java.io.IOException;

    import org.apache.poi.ss.usermodel.Cell;
    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;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class excelPOIcode
    {
    public static FileInputStream excelFile;
    public static XSSFWorkbook wb;
    public static XSSFSheet sheet;
    public static XSSFRow row;
    public static XSSFCell cell;

    public static void main(String[] args) throws IOException
    {
    String value = getCellData(3,1);
    System.out.println(value);
    String value1 = getCellData(4,1);
    System.out.println(value1);
    String value2 = getCellData(4,1);
    String writeData = setCellData(“Paryushan”,1,1);
    System.out.println(writeData);
    }

    // creating common method to call it pass row column values and use it thru’out the project
    public static String getCellData(int rowNum, int col) throws IOException
    {
    excelFile = new FileInputStream(“C:\Users\Dhruti\Desktop\autotest suite\Homedepotsearch.xlsx”);
    wb = new XSSFWorkbook(excelFile);
    sheet = wb.getSheet(“search”);
    row = sheet.getRow(rowNum);
    cell = row.getCell(col);
    return cell.getStringCellValue();

    }

    public static String setCellData(String colData, int rowNum1, int col1) throws IOException
    {
    excelFile = new FileInputStream(“C:\Users\Dhruti\Desktop\autotest suite\Homedepotsearch.xlsx”);
    wb = new XSSFWorkbook(excelFile);
    sheet = wb.getSheet(“type”);
    row = sheet.getRow(rowNum1);
    cell = row.getCell(col1);
    //String tcell = cell.setCellValue(colData);

    return setCellData(colData, rowNum1, col1);

    }
    }

    Reply
    • Mukesh Otwani says

      September 23, 2015 at 11:30 PM

      Hi Dhruti,

      I have created 2 video’s tutorials for this.
      Please watch and let me know if you still finding some issues while reading and writing excel.

      Read Excel



      Write Excel



      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

Video Player
https://www.youtube.com/watch?v=w_iPCT1ETO4
00:00
00:00
31:21
Use Up/Down Arrow keys to increase or decrease volume.

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?