java 后台生成Excel 返回浏览器直接下载

//返回给浏览器自动下载
    @RequestMapping(value = "/download", method = RequestMethod.GET, produces = "application/vnd.ms-excel")
    public void download(HSSFWorkbook workbook,HttpServletResponse response) {
    	 OutputStream output;
		try {
			
			List> errorExcel=new ArrayList>();
			List list = new ArrayList();
			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();
		}
	     
    }

需要注意的是  设置头

你可能感兴趣的:(java 后台生成Excel 返回浏览器直接下载)