简体   繁体   中英

Generic Export To Excel of java objects

In our spring project we have many controllers that receives client inputs(filters), this will create dynamic queries from the DB and the resultset is returned back to the client.

for example:

public List<UserResultDTO> getUsers(Filter filter);
public List<TransactionResultDTO> getTransactions(Filter filter);
public List<ProfileResultDTO> getProfile(Filter filter);

Our new requirmemnt is very simple : "Please allow an export of these results lists to an excel file"

The whole idea of exporting to an excel file is already taken care of. (We have a very robust Excel Provider)

So our goal basically is to create a very generic function\\service that will take a List and will be able to export it in a generic way.

Any ideas what the best practice for this kind of task?

Ideas in mind:

1) Create an enum that will contain all the configuration and column names for each model object (seems very redundant and maintanance hell)

2) Use reflection maybe? Maybe even using annotations for the fields for Excel-column-names

3) Other?

Thanks!

Certainly not an enum; that would add an artificial interdependancy: if some independent DTOs are changed, the enum is visited a couple of times.

There is the BeanInfo API , meta-information stored as Java class parallel to the DTO class. This is especially meant for this kind of situation. But maybe overkill.

So it probably comes down to your own adapter, giving a reasonable output using reflection. And autodetected improvements using declarative means:

  • (not so good) annotations in the DTOs (how are DTOs developed?)
  • XML or .properties with full class name; one XML per class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM