导出excel文件接口代码示例
1.该导出接口,token不能通过请求头来传输,需要在get请求的参数中带出来 2.验证token的方法除了在拦截器中统一拦截,针对get接口传参数的方式也需要单独在接口中验证。
代码语言:javascript复制@RequestMapping(value = "export", method = RequestMethod.GET)
public String export(ExportReq query,HttpServletResponse response) {
logger.info("导出Excel uri:{},param:{}","/export",query.toString());
try{
//业务逻辑处理,生成excel文件。
HSSFWorkbook export = myService.export(query);
if(export==null){
return "success";
}
//设置返回的文件流
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=MyExport_" (new Date()) ".xls");
OutputStream ouputStream = response.getOutputStream();
export.write(ouputStream);
ouputStream.flush();
ouputStream.close();
return null;
}catch(Exception e){
logger.info("导出失败...",e);
return "fail"
}
}