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
| Name | DOB |
|---|---|
| Employee_1 | 25-05-1989 |
| Employee_2 | 15-02-1980 |
| Employee_3 | 02-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?
Another way if you do not want to create any class.
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.🙏
Easy init...
Thanks for visiting.🙏
Excellent ! its very helpful
ReplyDeleteVery easy to use Now No Raw Apache POI to struggle.
ReplyDelete