0

I need a downloadedable excel file by clicking a link I use Spring and JExcel Api

This is controller:

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

And this JExcel Api 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;
}
...
}

Methods are executed but nothing happens on frontend in response. What is the problem?

UPD: I tried with AbstractExcelView, but it didn't change anything

 @RequestMapping(value = "/authorized/admin/scheduleReport", method = RequestMethod.GET)
   public View listExcell() {
    return new AbstractExcelView() {
        @Override
        protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook,
                HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFSheet sheet = workbook.createSheet("List of employees");

        setText(getCell(sheet, 0, 0), "Id");
        setText(getCell(sheet, 0, 1), "First name");
        setText(getCell(sheet, 0, 2), "Last name");
        setText(getCell(sheet, 0, 3), "Active");
        setText(getCell(sheet, 0, 4), "Salary");

        List<Employee> employees = dao.listEmployees();
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            setText(getCell(sheet, i + 1, 0), String.valueOf(employee.getId()));
            setText(getCell(sheet, i + 1, 1), employee.getFirstName());
            setText(getCell(sheet, i + 1, 2), employee.getLastName());
            setText(getCell(sheet, i + 1, 3), String.valueOf(employee.isActive()));
            setText(getCell(sheet, i + 1, 4), String.valueOf(employee.getSalary()));
        }
    }
};

}

cadmy
  • 515
  • 12
  • 32
  • Hard to say, something in the java logs? In your browser console? Can't you use the bundled "Excel view" (i.e. `AbstractJExcelView`)? –  Jan 20 '17 at 10:57
  • No errors in logs browser and server. I read about AbstractJExcelView but didn't understand concept clearly how it interacts with controller. – cadmy Jan 20 '17 at 11:05
  • How do you make the request, are you sure a request is made, what is the http status code you get back from the request? (as a side note: note that JExcelApi support will be dropped in future spring version, see the javadoc of `AbstractJExcelView`) –  Jan 20 '17 at 13:07

0 Answers0