//返回给浏览器自动下载
@RequestMapping(value = "/download", method = RequestMethod.GET, produces = "application/vnd.ms-excel")
public void download(HSSFWorkbook workbook,HttpServletResponse response) {
OutputStream output;
try {
List<List<String>> errorExcel=new ArrayList<List<String>>();
List<String> list = new ArrayList<String>();
list.add("add");
list.add("add");
list.add("add");
list.add("add");
errorExcel.add(list);
HSSFWorkbook workbook1 =PoiUtil.makeExcel(errorExcel);
output = response.getOutputStream();
//清空缓存
response.reset();
//定义浏览器响应表头,顺带定义下载名,比如students
response.setHeader("Content-disposition", "attachment;filename=result.xls");
//定义下载的类型,标明是excel文件
response.setContentType("application/vnd.ms-excel");
//这时候把创建好的excel写入到输出流
workbook.write(output);
//养成好习惯,出门记得随手关门
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
需要注意的是 设置头