A lightweight Java Framework that can be used to read XLSX Excel files. A lot of software applications contain features whose sole purpose is to just read data from XLSX files for which they need to rely on existing heavyweight frameworks with additional dependencies. This framework's sole purpose is to help users to read data from .xlsx files.
<dependency>
<groupId>com.github.kunalk16</groupId>
<artifactId>lightExcelReader</artifactId>
<version>1.0.2</version>
</dependency>
- com.sun.xml.bind » jaxb-core
- com.sun.xml.bind » jaxb-impl
- javax.activation » javax.activation-api
- javax.xml.bind » jaxb-api
- org.glassfish.jaxb » jaxb-runtime
- org.junit.jupiter » junit-jupiter-engine
- org.mockito » mockito-core
- org.mockito » mockito-junit-jupiter
- OpenJDK 11+
- XLSX
- Reading Excel files sheet by sheet, row by row, and cell by cell.
- Mapping Excel file data into Java object models.
This will allow you to read an XLSX file, generate a WorkBook object out of it. You can access available data on sheets, rows, and cells from that. Currently, data from all the cells are available in String format only, future additions may be made to return cell values in other types.
This makes use of the feature to read the excel files to convert your excel sheet to a list of POJO objects or convert your entire excel workbook to a POJO object containing a group of lists each containing values from individual excel sheets within the excel workbook.
To generate a WorkBook object from your .xlsx file, all you need to do is simply execute the following code:
WorkBook workBook = WorkBookFactory.create("D://file.xlsx");
- All cell values will be returned in
Stringformat. - Date values in cells may not be working properly(you can convert the cells in the excel file to text type to make this work).
- Excel file being read should not be open by Microsoft Excel or some other application when this framework is reading the data.
To generate a list of custom Java objects for a sheet in the excel file, you need to use the ExcelColumn annotation. In your model class, annotate the properties which you would like to bind with the excel data. For example:
class Employee {
@ExcelColumn(name = "First Name")
private String firstName;
@ExcelColumn(name = "Last Name")
private String lastName;
@ExcelColumn(name = "Address")
private String address;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getAddress() {
return address;
}
}
Now, simply use the ExcelObjectMapper class to convert a Sheet object to a List of Employee objects. The value of the name function of the @ExcelColumn annotation must be exactly the same as the expected header row cell value in the excel sheet.
- All the
@ExcelColumnannotated properties in the Java data model class should be of typeString. nameis case sensitive.
To generate a custom Java object model from the WorkBook you need to create a Java data model class containing List objects annotated with the @ExcelSheet. Each list object will represent rows within a sheet. Example:
class Company {
@ExcelSheet(name = "Employee")
private List<Employee> employees;
@ExcelSheet(name = "Staff")
private List<Staff> staff;
public List<Employee> getEmployees() {
return employees;
}
public List<Staff> getStaff() {
return staff;
}
}
The value of the name function of the @ExcelSheet annotation should be the exact sheet name of the excel file.
- The first non-empty row of every sheet must be the header row.
nameis case sensitive.
In case the required objects are not generated, a null value or an empty Listis returned. Framework currently does not percolate the exceptions thrown but unstead logs exceptions. Logging is disabled by default, but in order to enable or disable logging, simply use the ExcelReaderLogger.enableLogging(true). The parameter accepts boolean type to enable or disable logging.