我需要一个可下载的excel文件,点击我使用Spring和JExcel Api的链接
这是控制器:
@RequestMapping(value = "/download", method = RequestMethod.GET)
public ModelAndView generateScheduleReport(HttpServletResponse response) {
reportService.createExcelOutputExcel(response);
return null;
}
这个JExcel Api服务:
@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;
}
...
}
执行方法但前端没有任何反应。问题是什么?
UPD: 我尝试使用AbstractExcelView,但它没有改变任何东西
@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()));
}
}
};
}