导出数据有多种方式,如POI等。这是一个导出全部list的控制器代码:
@Controller public class EmpController { @Resource private EmpService empService; @RequestMapping("/export") public String ExporExcel(HttpServletResponse response) throws Exception { //throws IOException { OutputStream out = re(); ExcelWriter writer =null; String fileName = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); try { fileName =(new Date()); //添加响应头信息 re("Content-disposition", "attachment; filename=" + fileName+"ex;); re("application/msexcel;charset=UTF-8");//设置类型 re("Pragma", "No-cache");//设置头 re("Cache-Control", "no-cache");//设置头 re("Expires", 0);//设置日期头 Ea(out,Emp.class).sheet("导出数据").doWrite()); wri(); out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { re().close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
说明:Ea方法中的几个参数是很重要的,如表头的设置、数据源等。