Я хочу создать отчет, в который будут включены все лицензии и страховки из их отдельных репозиториев в таблицу Excel. Есть ли способ сделать это так:
@RequestMapping(value="/report/expirationReport")
public void getExpirationReport(Model model,HttpServletResponse response){
List<License> licenses;
List<Insurance> insurances;
licenses = licenseRepository.findAll();
insurances = insuranceRepository.findAll();
List<String> headers=Arrays.asList("Legal Name","Principle Name","Type","State","Expiration");
response.addHeader("Content-disposition", "attachment; filename=ExpirationReport.xls");
response.setContentType("application/vnd.ms-excel");
try {
new SimpleExporter().gridExport(headers, licenses, insurances,"client.legalName, client.principleName,type,state,expiration", response.getOutputStream());
response.flushBuffer();
}catch (IOException e) {
e.printStackTrace();
}
}
Оба репозитория уже существуют, но я не могу просто добавить страховки (как я делал выше), потому что SimpleExporter, кажется, принимает только два объекта, а затем объект поддерживает. Есть идеи, как заставить его принять все три объекта? Или есть идеи, как наилучшим образом объединить / сохранить результаты двух функций findAll репо в один объект данных?
Edit:
Я смог заставить это работать, пройдя через таблицу «Клиент», так как у лицензии и страховки были внешние ключи для клиента. Вот код:
@RequestMapping(value="/report/expirationReport")
public void expirationReport(HttpServletResponse response){
List<Client> clients=clientRepository.findAll();
try {
response.addHeader("Content-disposition", "attachment; filename=expirationReport.xlsx");
response.setContentType("application/vnd.ms-excel");
InputStream is= new ClassPathResource("static/reports/expirationReport.xlsx").getInputStream();
Context context= new Context();
context.putVar("clients", clients);
JxlsHelper.getInstance().processTemplate(is,response.getOutputStream(),context);
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}
}