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

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com