Skip to content
Advertisement

Reading excel file columns based on header in Spring Batch

We have requirement to read the huge excel file in java. I am prefering Spring Batch for same ,as we have spring already in project. However, we have one bottleneck that this excel file can have different columns, user can change the order of columns . So, we have to identify which column has what data from first row / header.

Example :-

User 1 :
                    Name    EmployeeId
                    Raj      1
                   Peter     2

User 2 :-
                    EmployeeId Name
                   5                   Steven
                   6                   Antony

But in spring Batch ,we need to tell column order while configuring job. One way is that I will open file and read first row and configure Spring batch but that is not efficient. There should be some inbuilt way, however I am not able to find it.

Advertisement

Answer

The Spring Batch Excel extension : https://github.com/mdeinum/spring-batch-extensions/tree/master/spring-batch-excel has a RowNumberColumnNameExtractor which fits your needs

i.e. it reads the row 0 as a column row and can then be used in conjunction with a BeanWrapperRowMapper to map to a java object

Advertisement