It is necessary to make a report in excel, which would be loaded by clicking on the link, using Spring MVC and JExcel Api. When you click on the link, the controller catches the transition and the report generation method in the service starts, however, as a result, nothing happens in the browser. What's wrong?

Controller:

@RequestMapping(value = "/download", method = RequestMethod.GET) public ModelAndView generateScheduleReport(HttpServletResponse response) { reportService.createExcelOutputExcel(response); return null; } 

Service:

 @Slf4j @Service public class ReportService implements ExcelOutputService{ public WritableWorkbook createExcelOutputExcel(HttpServletResponse response) { String fileName = "Excel_Output.xls"; WritableWorkbook writableWorkbook = null; try { response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); writableWorkbook = Workbook.createWorkbook(response.getOutputStream()); WritableSheet excelOutputsheet = writableWorkbook.createSheet("Excel Output", 0); addExcelOutputHeader(excelOutputsheet); writeExcelOutputData(excelOutputsheet); writableWorkbook.write(); writableWorkbook.close(); } catch (Exception e) { log.error("Error occured while creating Excel file", e); } return writableWorkbook; } ... } 
  • Returns on the idea through the http response - cadmy
  • maybe this will help: ru.stackoverflow.com/questions/610071/… - Mikhail Rebrov
  • Specifically, in the controller you do not return the already created file, use the try / catch block directly to the controller api, and if you successfully pass to finaly, return the link to the file - GenCloud

0