TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Apache POI

Read Excel File using Apache POI

Overview

In this post, we will write a simple Java program to read an excel file using Apache POI.

First of all, let us create a simple excel file with data to read. Let’s use a sample Excel sheet with movie names with 2 columns. We will read the data from the Excel file in this tutorial. The filename of the Excel file is TDocs.xlsx.

 

ApachePoi_ReadExcelRow

Program to Read an Excel Row

The simple method to get one single row is shown below:

public ArrayList<String> getMyRow(int rowIndex, int endColIndex,int sheetIndex) throws IOException{
        ArrayList<String> rsData = new ArrayList<String>();
        try {
            file = new File("TDocs.xlsx");
            fis = new FileInputStream(file);
            workbook = new XSSFWorkbook(fis);
            sheet = workbook.getSheetAt(sheetIndex);
            for(int colStartPosition=0; colStartPosition<=endColIndex; colStartPosition++){
                Cell cell = sheet.getRow(rowIndex).getCell(colStartPosition);

            switch(cell.getCellType()) {
            case Cell.CELL_TYPE_STRING: 
            rsData.add(cell.getStringCellValue());
            break;
            }
            }
       
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            fis.close();
            }
        return rsData;
    }

 

 

Now, consume this method to read the data by passing the row index, columns in the main method.

public static void main(String... args) throws IOException
    {
        ArrayList<String> consume = new ArrayList<String>();
        ReadXL readexcel = new ReadXL();
        consume = readexcel.getMyRow(1, 1, 0);
        System.out.println("**********OutPut************");
        for(String s : consume)
        {
            System.out.print(s);
        }
        
    }

 

Run output of the program is displayed below :

**********OutPut************
TheIntern

 

Read Excel File

 

That’s it. We have successfully read data from an existing Excel file.  This is useful in Selenium Automation for reading test data from an excel file.

 

—

Apache POI Tutorials:

https://www.testingdocs.com/apache-poi-tutorials/

More Information on Apache POI API:

https://poi.apache.org/

Related Posts

Configure Build Path

Apache POI /

Add Apache POI To Java Project

Apache POI Download

Apache POI /

Download Apache POI

Update Excel File

Apache POI /

Update Excel File using Apache POI

Excel_Sheet_Movies_Names

Apache POI /

Getting number of Rows count from an Excel sheet

MS Excel Java Program

Apache POI /

Write to an MS Excel File using Apache POI

‹ Add Apache POI To Java Project

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version