Skip to main content

Read Excel with Java

Happy Automation!

How to do in plain Java and POI 

We use Apache POI library for reading an excel file.
To read a any excel file we required lots of steps.
1. Open a file
2. Create an object of workbook
3. Get a sheet
4. Get rows using loop
5. Get cells using nested loop.

But there is an easy way called exclim

How to use

Link to exclim
<dependency>
  <groupId>com.github.sukhjindersukh</groupId>
  <artifactId>Exlim</artifactId>
  <version>1.1</version>
</dependency>

Suppose we have Employee sheet in some excel file with following columns
NameDOB
Employee_125-05-1989
Employee_215-02-1980
Employee_302-05-2000

Now read all data in java

1. Create a simple java class as below
public class Employee {  
        String Name, DOB;  
    
      public String getName() {  
          return Name;  
      }  
    
      public void setName(String name) {  
          Name = name;  
      }  
    
      public String getDOB() {  
          return DOB;  
      }  
    
      public void setDOB(String DOB) {  
          this.DOB = DOB;  
      }  
    
      @Override  
    public String toString() {  
          return "Employee{" +  
                  "Name='" + Name + '\'' +  
                  ", DOB='" + DOB + '\'' +  
                  '}';  
      }  
  }

2. Create another java class with main method and use following code.

Exl exl = new Exl();  
exl.setDateDataFormat("MM-dd-yyy");  
String path = "src/test/resources/TestData.xlsx";  
List<Employee> employees = exl.read(Employee.class, path);  
for (Employee employee : employees) {  
      System.out.println(employee.toString());  
}
That's it!
Why exclm easy?

1. We don't need to pass a sheet name but class name should be same. 
2. All records from excel stored inside List

3. If you want to filter some specific data object then use condition inside the loop like.    if(employee.getName().equal("Employee_2")) {//return this object }

Another way if you do not want to create any class.
Exl exl = new Exl();
exl.openWorkbook(path);
Recordset recordset =exl.getRecords("Employee");
exl.closeWorkbook();
List<Recordset.Record> records = recordset.getRecords();
for(Recordset.Record record:records){
   System.out.println(record.getValue("Name"));
}

Easy init... 

Thanks for visiting.🙏

Comments

  1. Very easy to use Now No Raw Apache POI to struggle.

    ReplyDelete

Post a Comment

Popular posts from this blog

Quick Start with Robot Framework: Simplifying Test Automation

 Introduction: Are you tired of spending hours on repetitive software testing tasks? Look no further! Robot Framework is here to simplify your test automation process. In this quick start guide, we'll explore the key features of Robot Framework and provide you with the necessary steps to get started. What is Robot Framework? Robot Framework is an open-source test automation framework that allows you to write easy-to-read test cases in a keyword-driven format. It provides a simple and intuitive way to automate your software tests, making it a popular choice among testers and developers. Installation: To begin using Robot Framework, follow these installation steps: Install Python on your machine (version 2.7 or higher). Use pip, the Python package installer, to install Robot Framework: > pip install robotframework Install Selenium library >  pip install  robotframework-seleniumlibrary Creating Your First Test Case: Let's dive into creating your first test case with Ro...